Search code examples
iosuiwebview

webVIew.request.URL in didFailLoadWithError: is the previous URL not the failed URL


Suppose there is a webpage A.html that contains a link to B.html.

If B.html is clicked then request.URL in shouldStartLoadWithRequest: will be B.html as it should be. However if there is a problem loading that page (suppose it doesn't exist for example) then in didFailLoadWithError: the value of webView.request.URL is not B.html but A.html.

Therefore it seems its not possible to know which page load failed unless I cache the last page load, but I would have expected webView.request.URL to be B.html, therefore is this a defect? I didn't see documentation on what it should be.

[iOS 6]


Solution

  • I had the same problem. If anyone else does too, error.userInfo dictionary works instead.

    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    
    {
    
        if (error.domain == NSURLErrorDomain) {
            if (error.code == NSURLErrorCancelled) { //ignore this one, interrupted load
                return;
            }
    }
    }
    //NSString *theURLString = [webView.request.URL absoluteString]; this won't work - it just returns the last successful url
    NSString *theURLString = [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]; //this works
    

    The doco says NSErrorFailingURLStringKey is deprecated in iOS4 (only provided for backwards compatibility) and you should use NSURLErrorFailingURLStringErrorKey instead.

    However NSURLErrorFailingURLStringErrorKey isn't returned (not by my version of UIWebView anyway). Instead, NSErrorFailingURLKey is another key returning the URL, but I can't find that in the documentation anywhere.