Search code examples
iphoneuiwebviewuiwebviewdelegate

Detect and Customize UIWebView's "Unable to Read Document" error


I'm testing a UIWebview with a number of different document types - .xlsx, .jpg, etc. - and it opens most of them just fine. From time to time, I open a local file and this message appears right in the web view:

Unable to Read Document
An error occurred while reading the document

I'm not concerned with "why" this error occurs - it happens, for instance, when I feed the UIWebView a garbage file (intentionally). The problem is that I can't figure out how to detect "when" this happens. It doesn't trigger webView:didFailLoadWithError, it doesn't trigger an NSException (via @try & @catch), and when I inspect the document in webViewDidFinishLoad, webView.request.HTTPBody is null.

Anyone know how to detect when UIWebView can't display content?


Solution

  • just call uiwebview delegate method 
    
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
    
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSString *html = [webView stringByEvaluatingJavaScriptFromString:
                          @"document.body.innerHTML"];
        if ([html rangeOfString:@"Unable to Read Document."].location == NSNotFound) {
    
           NSLog(@"NO Error");
    
        }
        else
        {
    //        NSLog(@"File contains Error");
        }
    
    }