Question

I noticed a weird bug in my web view. I have an app that checks to see if the device is connected to the internet, if it is connected then the error image remains hidden. If it is not connected, I unhide the error image. The thing is, if I try and click on another link before the page is finished loading, didFailLoadWithError: fires and unhides the connection issue image. Here is the code I'm using:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL],
          (webView.loading?@"NO":@"YES"));
}

-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {

    NSLog(@"No internet connection");
    _connectionError.hidden = NO;
}

If I wait until I see "still loading" in the log, then press another link, all is well. If I press a link before I see that message, then didFailLoadWithError:fires and unhides the connection issue. I'm extremely new to Objective-C, and programming in general, so I have no idea how to fix this.

Is there any simple that I can have didFailLoadWithError: only run once when the app starts up?

Was it helpful?

Solution 2

The solution that I used was to check for the error code. If the code equaled the connection error, then I unhid the error image.

OTHER TIPS

The didFailLoadWithError is called everytime the web view failed to load, which happens when you try to load another page before the current page is finished loading.

You should try other methods to detect if there is no internet connection, such as using Reachability: http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

Another way is simply calling stopLoading in webView:shouldStartLoadWithRequest:navigationType: delegate method for your particular case of triggering didFailLoadWithError when user taps on another link while the previous link is still loading in the web view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top