简体   繁体   中英

UIWebView loadRequest: continuously failing with NSURLErrorTimedout in iOS 6

There is an issue where native connection in UIWebView starts failing with timeout. Once timeout starts, only hard exiting app will solve it.

Timeout starts randomly but once started, only hard exit will solve it.

Since, hard exit is solving it, it is a client and not server issue. But with the available API of UIWebView I can't figure out the problem.

It is till now seen only in iOS6 iPhone and iPad. I am hitting same URL every time and I am caching JS, CSS resources (maybe something is wrong with iOS6 webview cache).

//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];
    }
}

Can you change NSURL code line like this:

NSURL * url = [NSURL URLWithString:rephrasing[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Maybe it will solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM