I'm trying to figure out the most appropriate way to handle errors that can occur when loading pages in a UIWebView
.
I'd like to alert the user if I notice a network related issue, or a server related issue. I am unable to find any details on the specific error codes to check for. This is what I have right now:
NSInteger errorCode = [error code];
NSString* title = nil;
NSString* message = nil;
if (errorCode == NSURLErrorNetworkConnectionLost || errorCode == NSURLErrorNotConnectedToInternet) {
title = @"Error";
message = @"The network connection appears to be offline.";
}
if (errorCode == NSURLErrorTimedOut || errorCode == NSURLErrorBadServerResponse) {
title = @"Error";
message = @"There was an error loading the request. Please try again later.";
}
if (title != nil) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
alert.tag = TAG_WEB_ERROR;
[alert show];
}
Am I checking for the correct error codes? Any thoughts on a better way to check for and handle potential errors?
Some of the error codes are defined in NSURLError.h
e.g. NSURLErrorTimedOut
if ([error.domain isEqualToString:NSURLErrorDomain]) {
if(error.code == NSURLErrorTimedOut) {
...
}
}
Not sure if that's full set of error codes returned by UIWebView. And there is no such thing like NSError-s list, you need to check codes and domains.
Assuming that you are using the UIWebView in a view controller , you can set the view controller to be the delegate of the UIWebView and implement the following method from the UIWebViewDelegate protocol
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
alert.tag = TAG_WEB_ERROR;
[alert show];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With