UIWebView loadRequest:iOS 6中的NSURLErrorTimedout连续失败

8su*_*has 5 iphone timeout uiwebview ios ios6

存在UIWebView 启动中的本机连接因超时而失败的问题.一旦超时开始,只有硬退出的应用程序才能解决它.

超时随机开始,但一旦启动,只有硬退出才能解决它.

因为硬退出正在解决它,它是一个客户端而不是服务器问题.但是用UIWebView我可用的API 无法弄清楚问题.

到目前为止只能在iOS6 iPhone和iPad上看到它.我每次都在点击相同的URL,我正在缓存JS,CSS资源(可能是iOS6 webview缓存有问题).

//code
//usual webview loading code 
//except I am setting cookies everytime before load request 
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webview.delegate = self;


    NSString * urlString;

    urlString = @"https://www.myserver.com/";

    NSURL * url = [NSURL URLWithString:urlString];


    // I create and set some cookies here. 
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:self.mobilePageURL mainDocumentURL:self.mobilePageURL];


    [self.webview loadRequest:[NSURLRequest requestWithURL:url]];

}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if([error.domain isEqualToString:NSURLErrorDomain] && error.code != NSURLErrorCancelled)
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

     UIAlertView * alert  =  [[UIAlertView alloc] init];

     NSString * errorMessage = [NSString stringWithFormat:@"%d",error.code];

     [alert setMessage: errorMessage];
     [alert addButtonWithTitle:@"Ok"];
         [alert show];
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以像这样更改 NSURL 代码行吗:

NSURL * url = [NSURL URLWithString:rephrasing[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Run Code Online (Sandbox Code Playgroud)

也许它能解决你的问题