Having `didFailLoadWithError:` only run once on startup

239 views Asked by At

If I try and click on another link before the page is finished loading, didFailLoadWithError: fires and unhides a connection issue image that I hid. 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;
}

Does anyone know of a quick way to have it only run once on the apps startup and never run again for the remainder of the time?

1

There are 1 answers

1
d00dle On BEST ANSWER

This is a delegate method that is fired everytime it fails..

try something like this. (create a BOOL named firstTimeRun in header)

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

    if (firstTimeRun) {

        _connectionError.hidden = NO;
        firstTimeRun = NO;
    }
}

Remember to set firstTimeRun to NO when site is loaded if you only want this on the first load.

EDIT:

In your Header file (.h) you write the following

@interface ... {

@property BOOL firstTimeRun;

}

you might need to initialise it with NO somewhere in your method. You need to post your .h and .m files of the class before I can help you more.