0

In the app that I'm writing, I check to see if the device has an internet connection. I put a connection error image over the screen, and hide it unless the device is not connected. There is an odd issue though. I implemented a simple back button for the UIWebView, but when I press it too fast, the connection error occurs. Here is the code I use to check for connection, and decide whether to display the error:

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

    _connectionError.hidden = NO;
}

So, I think the only way to solve this issue would be to have it check if there is a connection one time, only when the app first launches, and never run again for the remainder of the time. I'm extremely new to Objective-C, and have no idea how to do this. I'm thinking that I should put something in viewDidLoad, or implement some way to have the method run only once, but I have no idea how to do that.

Here's the code for the back button:

- (IBAction)backButtonTapped:(id)sender {
    [_viewWeb goBack];
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Henry F
  • 4,960
  • 11
  • 55
  • 98

2 Answers2

1

To check for a connection you can use Reachability in your project. You can then use this answer to see how to use it. This would be more efficient and cleaner than using a UIWebview.

Community
  • 1
  • 1
jfuellert
  • 550
  • 4
  • 10
  • Thanks for the answer. It appears that Reachability uses push notifications though to display the message, is this correct? – Henry F May 29 '13 at 01:24
  • It doesn't have to. You can check `if(([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable))` for example, to check if there is an internet connection available. This can be checked from anywhere after importing `Reachability.h`. – jfuellert May 29 '13 at 01:29
  • @Oral B I should add that you can try using this method before trying to actually load a web page; doing so could save you some hassle – jfuellert May 29 '13 at 01:47
1

Call the method stopLoading on the webView before the goBack method to make sure there is no multiple request going which can cause the connection error:

- (IBAction)backButtonTapped:(id)sender {
    [_viewWeb stopLoading];
    [_viewWeb goBack];
}
Valent Richie
  • 5,226
  • 1
  • 20
  • 21