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

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

问题描述

我正在用多种不同的文档类型(.xlsx,.jpg等)测试UIWebview,它可以很好地打开大多数文档.有时,我会打开一个本地文件,并且此消息会直接出现在网络视图中:

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天全站免登陆