UIWebView not go to didFailLoadWithError when webLink not found?

Jack picture Jack · Jan 22, 2013 · Viewed 17.9k times · Source

I use UIWebView to load web from a webLink and UIWebViewDelegate to control error state:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webLink]]];


- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"START LOAD");

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"FINISH LOAD");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"ERROR : %@",error);
}

But when webLink not found, it NOT go to didFailLoadWithError, it go to startLoad and didFinishLoad. How to go the situation when webLink not found? Please help!

Answer

Rob picture Rob · Jan 22, 2013

Unfortunately, 404 (or similar codes) aren't considered errors by UIWebView, because a HTML response was received. Worse, the UIWebView doesn't capture response codes for us, so you have to do that manually, via NSURLConnection. Here's one way to deal with it:

@interface ViewController () <UIWebViewDelegate, NSURLConnectionDataDelegate>

@property (nonatomic) BOOL validatedRequest;
@property (nonatomic, strong) NSURL *originalUrl;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // since `shouldStartLoadWithRequest` only validates when a user clicks on a link, we'll bypass that
    // here and go right to the `NSURLConnection`, which will validate the request, and if good, it will
    // load the web view for us.

    self.originalUrl = [NSURL URLWithString:@"http://www.stackoverflow.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:self.originalUrl];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

#pragma mark - UIWebViewDelegate

// you will see this called for 404 errors

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.validatedRequest = NO; // reset this for the next link the user clicks on
}

// you will not see this called for 404 errors

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%s error=%@", __FUNCTION__, error);
}

// this is where you could, intercept HTML requests and route them through
// NSURLConnection, to see if the server responds successfully.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // we're only validating links we click on; if we validated that successfully, though, let's just go open it
    // nb: we're only validating links we click on because some sites initiate additional html requests of
    // their own, and don't want to get involved in mediating each and every server request; we're only
    // going to concern ourselves with those links the user clicks on.

    if (self.validatedRequest || navigationType != UIWebViewNavigationTypeLinkClicked)
        return YES;

    // if user clicked on a link and we haven't validated it yet, let's do so

    self.originalUrl = request.URL;

    [NSURLConnection connectionWithRequest:request delegate:self];

    // and if we're validating, don't bother to have the web view load it yet ...
    // the `didReceiveResponse` will do that for us once the connection has been validated

    return NO;
}

#pragma mark - NSURLConnectionDataDelegate method

// This code inspired by http://www.ardalahmet.com/2011/08/18/how-to-detect-and-handle-http-status-codes-in-uiwebviews/
// Given that some ISPs do redirects that one might otherwise prefer to see handled as errors, I'm also checking
// to see if the original URL's host matches the response's URL. This logic may be too restrictive (some valid redirects
// will be rejected, such as www.adobephotoshop.com which redirects you to www.adobe.com), but does capture the ISP
// redirect problem I am concerned about.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{     
    NSString *originalUrlHostName = self.originalUrl.host;
    NSString *responseUrlHostName = response.URL.host;

    NSRange originalInResponse = [responseUrlHostName rangeOfString:originalUrlHostName]; // handle where we went to "apple.com" and got redirected to "www.apple.com"
    NSRange responseInOriginal = [originalUrlHostName rangeOfString:responseUrlHostName]; // handle where we went to "www.stackoverflow.com" and got redirected to "stackoverflow.com"

    if (originalInResponse.location == NSNotFound && responseInOriginal.location == NSNotFound) {
        NSLog(@"%s you were redirected from %@ to %@", __FUNCTION__, self.originalUrl.absoluteString, response.URL.absoluteString);
    }

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

        if (statusCode < 200 || statusCode >= 300) {
            NSLog(@"%s request to %@ failed with statusCode=%d", __FUNCTION__, response.URL.absoluteString, statusCode);
        } else {
            self.validatedRequest = YES;

            [self.webView loadRequest:connection.originalRequest];
        }
    }

    [connection cancel];
}

@end

Note, in my implementation, I'm not only checking for status codes, but I'm also checking for redirects (which you may or may not want to do). I do this because some ISP's intercept HTTP requests, and if the destination site is not found, redirect you to their own search web page (which I think is a bit creepy knowing that my ISP is checking out every web site I search for). And if you're dealing with iPhones that are connecting via wifi, you have to deal with these vagaries.

So, for example, my code above is searching for "http://www.applecom/pages" (in which I deliberately omitted the period of ".com", which should fail a DNS lookup), but for which my ISP, Verizon, intercepted the request and redirected by HTTP connection to their own search page and as such, my app is reporting:

2013-01-21 23:14:21.896 webtest[24198:c07] -[ViewController connection:didReceiveResponse:] you were redirected from http://www.applecom/pages to http://search.dnsassist.verizon.net/assist.php?url=www.applecom

You might want to think about what sort of redirects are acceptable (e.g., if you go to "www.adobephotoshop.com" and it redirects you to "www.adobe.com") and what sorts aren't (e.g., if I go to "www.applecom" and it redirects me to "search.dnsassist.verizon.net". I may be worrying about a fairly narrow problem (which affects me because of my ISP), but it's something to contemplate.