检测并自定义UIWebView的“无法读取文档”错误 [英] Detect and Customize UIWebView's "Unable to Read Document" error

查看:248
本文介绍了检测并自定义UIWebView的“无法读取文档”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个包含许多不同文档类型的UIWebview - .xlsx,.jpg等 - 它打开了大部分文档就好了。我不时会打开一个本地文件,这条消息会出现在网页视图中:

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

我不关心为什么发生此错误 - 例如,当我向UIWebView提供垃圾文件(故意)时,就会发生这种情况。问题是我无法弄清楚如何检测何时发生这种情况。它不会触发 webView:didFailLoadWithError ,它不会触发 NSException (通过 @try & @catch ),当我在 webViewDidFinishLoad 中检查文档时, webView.request.HTTPBody 为空。

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.

任何人都知道如何检测UIWebView何时无法显示内容?

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

推荐答案

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

}

这篇关于检测并自定义UIWebView的“无法读取文档”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆