Search code examples
iosobjective-cios7uiwebviewuiwebviewdelegate

Random error on loading a static resource with a UIWebView


I'm using a UIWebView to load a static resource shipped within my application bundle. Sometimes, I have no clear what the problem could be, I receive the following error within the delegate method - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1001

The resource is loaded through a NSURLRequest where I set up a timeout interval of 10 seconds, but that interval is not followed. In fact, in the debug console I'm able to see that the error delegate is called after about 2 seconds.

NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"root" ofType:@"html"] isDirectory:NO];
NSURLRequest* htmlRequest = [NSURLRequest requestWithURL:htmlFile cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];

[webView loadRequest:htmlRequest];

The fact is that I cannot always replicate the problem I have. Any suggestions?

P.S. I'm working with an app that runs from 4.3. The problem is also with iOS 7.


Solution

  • According to apple CFNetwork Error Codes Reference this code is a timeout error:

    kCFURLErrorTimedOut  = -1001
    

    Timeout are not always easy to reproduce most likely you should extend the timeout value when you init the NSURLRequest.

    According to the same docs you can query the object for additional information.

    For example:

    if (CFEqual(CFErrorGetDomain(err), kCFErrorDomainCFNetwork) && CFErrorGetCode(err) == kCFHostErrorUnknown) {
    
        CFDictionaryRef userInfo = CFErrorCopyUserInfo(err);
    
        CFNumberRef number = (CFNumberRef) CFDictionaryGetValue(userInfo, kCFGetAddrInfoFailureKey);
    
        ...
    
        CFRelease(userInfo);
    }