Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView - Error Handling Best Practices [closed]

Tags:

ios

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?

like image 877
Steve Avatar asked Oct 03 '14 12:10

Steve


2 Answers

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.

like image 190
Krzysztof Avatar answered Nov 09 '22 20:11

Krzysztof


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];
}
like image 1
Anand Biligiri Avatar answered Nov 09 '22 22:11

Anand Biligiri