I have a native iPhone app that just loads a website using UIWebView. I implemented the UIWebViewDelegate function didFailLoadWithError to display an error message when the user attempts to use the app without an Internet connection. The alert box shows up the first time I build and run the app, but does not appear thereafter. If I click the home button, then tap my app again, I just see a blank UIWebView. It’s as if it’s not trying to load the page again, so it never triggers didFailLoadWithError. This is just a simple app without saving and reloading of state, so shouldn’t reloading the app re-run all my code, including the attempted loading of the website?
- (void)viewDidLoad {
NSString* urlString = [NSString stringWithString:@"http://www.site.com"];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[super viewDidLoad];
}
#pragma mark UIWebView delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//[myActivityIndicatorView stopAnimating];
NSLog([NSString stringWithFormat: @"%d", [error code]]);
if ([error code] != USER_STOPPED_LOAD) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Connection Error!"
message:@"You must be connected to the Internet to use Site."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
No because it only gets sent to the background when you click the Home button! To restart the app, kill it using the multitasking bar.