5

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?

Steve
  • 53,375
  • 33
  • 96
  • 141

3 Answers3

4

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.

Krzysztof
  • 1,441
  • 11
  • 22
  • Thanks. So is it just assumed that all developers figure out the potential errors by trial and error? I guess I figured Apple docs would list the codes. – Steve Oct 03 '14 at 13:09
  • I just handle 2 kind of errors most of the case. App error or any other error, so don't need to know exactly what was the error. Why do you need to know what was the exactly the error ? – Krzysztof Oct 03 '14 at 13:12
  • I'm new to iOS so forgive any ignorance. However, I read that they're are potential "errors" that can be raised if you attempt to load a page, but back out before it loads for example, among others. I do not want to alert the user when something that is not a true error occurs. – Steve Oct 03 '14 at 13:15
  • Ah i see, guess you might want to change the question then. What i do is i wait for load to finish, if page is loaded any errors are ignored. So you get errors say on redirect or stuff, but main content is loaded. Hope that helps. Also check this one: http://stackoverflow.com/a/15916853/2754158 – Krzysztof Oct 03 '14 at 13:20
1

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];
}
Anand Biligiri
  • 241
  • 1
  • 3
0

Perhaps not directly answering your question (Am I checking for the correct error codes?), but Mattt Thompson has a great rundown at NSHipster of NSError in the context of both NSURLErrorDomain and CFNetworkErrors.

The list contains possible error codes, error domains and associated reasons in a neatly-formatted table.

Chris Droukas
  • 3,196
  • 1
  • 23
  • 26