Detect and Customize UIWebView's “Unable to Read Document” error

最后都变了- 提交于 2020-01-02 04:12:08

问题


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?


回答1:


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");
    }

}



回答2:


If inspecting the log does not show anything out of the ordinary, run it on the actual device. As a bit of help, my log says

Cannot find data converter callback for uti public.data
Failed to generate preview

which means that the simulator is failing. After being frustrated with this same problem, I went thru the whole final certification and installation and provisioning process, and too confirm that Word 97 and Pages / Pages.zip files containing text do indeed display just fine on the actual device. SOLVED. The simulator itself is broken, which is very...troubling, that this little note didn't seem to make it into the release notes, and also complicates development a tad bit. However, the work around is to select the Device in Xcode and deploy out, and it should work.



来源:https://stackoverflow.com/questions/4271379/detect-and-customize-uiwebviews-unable-to-read-document-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!