iosobjective-cuiwebviewwkwebviewrequest-timed-out

iOS UIWebView loadRequest takes too long


I have one UIWebView in my app,

-(void)viewDidLaod{
    NSURL *url = [NSURL URLWithString:@"some url string"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30.0];        
    myWbView.delegate = self;
        [myWebView loadRequest:request];

        if ([timer isValid]) {
            [timer invalidate];
        }else{
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(addSec:) userInfo:nil repeats:YES];
             //counting seconds
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView.isloading){
    NSLog(@"LOADING");//it takes 5-8 seconds in average keep logging this
    }else{
    NSLog(@"FINISH");//since the request started,8-15 seconds later this is logged out
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"LOAD FAIL: %@",error);
}

here's the webpage I am trying to load, I put the webview controller in a pageViewController that every time I swipe to the next page a new load request starts.

(I do stop any unfinished loading when the swiped out views dealloc, so it won't be blocking the web tread)

sometimes i got some error messages like:

error from didFialLoadWithError

As a newbie to app development, I totally have no idea what's wrong with my code, is it some thread got stuck or the webpage using JavaScript/HTML5 that matters the speed?

I had looked into the Flipboard app (iOS7 version) which uses webview to load content page,it responses immediately after user taps any post on it. Even if the loading speed of UIWebView is normally slow, a faster performance is possible.

Any suggested guide link / background knowledge I should have known would be appreciated.

PS: Since another better stuff WKWebView is only supported in iOS8 and later (in my app WKWebView loads in the same speed as UIWebView, but scores higher at HTML5 test site), I still need UIWebView for iOS7.


Solution

  • Well, the NSLocalizedDescription of the error you printed says what's going on: "Too many HTTP redirects."

    When you try to access a web page, rather than giving you the page you're trying to reach, the server can reply with a redirect—an address for a different page you should go to instead. For instance, accessing a shortened link will typically redirect you to the real page; a password-protected website might redirect you to a sign-in page; or a site that's been redesigned might redirect old addresses to their new locations.

    However, if not implemented carefully, this feature could end up locking up a browser by redirecting it infinitely: for instance, imagine if www.example.com/a redirected you to www.example.com/b, and www.example.com/b redirected you back to www.example.com/a. So browsers impose a limit on the number of redirects they'll follow—usually about ten or fifteen—to avoid getting stuck in these kinds of loops.

    The error you're getting means that, for some reason, accessing that page is causing your browser to be redirected many times in a row—so many that it just assumed something must be wrong and gave up.

    Sitting here without a copy of your app to examine, I can't tell you what those redirects are, let alone why they're being issued, but you can at least answer the first question using the Safari Web Inspector. You'll find it in Safari's "Develop" menu, which can be enabled in Safari preferences; there are options in that menu to inspect web pages in iOS devices or simulators. You may have to turn the Web Inspector option on in the device's or simulator's Safari settings as well.

    The Web Inspector's Timeline tool can tell you what redirects you're getting sent through. Once you know that, you may be able to figure out why it's doing what it's doing, and maybe even tweak your app to work around the issue. (For example, the user agent string your UIWebView is using might be confusing the servers involved. This is a complete guess, though, so don't assume that that's what's going on here.)