iPhone开发-UIWebView关闭自动检测数据

来源:互联网 发布:坚果资本 知乎 编辑:程序博客网 时间:2024/06/05 19:57

假设你网页中有一段话,中间内容一部分的内容是www.yesareno.com,UIWebview会给你自动识别成http://www.yesareno.com,当然大部分情况下是好的,但是也有一些特殊情况,你不像把www.yesareno.com识别成超链接(即点击无反应,先不管这变态的需求吧),如何实现呢?

</pre><p></p><p>下面是网页的源代码:</p><p></p><pre name="code" class="html"><body><div id="content"><p><span style="font-family: 微软雅黑, 'Microsoft YaHei'; font-size: 28px;">如需帮助,请移步到官网 www.yesareno.cn解决。</span></p><p><br/></p></div></body>

当点击网页中的www.yesareno.com时,会调用webview的代理:

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {// Determine if we want the system to handle it.NSURL *url = request.URL;}

此时打印url.schema是http,这就是webview自作聪明添加的http。那怎么破呢?

经过查看文档,发现webview有自动检测数据的属性,即 dataDetectorTypes, 其中包括 

typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {    UIDataDetectorTypePhoneNumber   = 1 << 0,          // Phone number detection    UIDataDetectorTypeLink          = 1 << 1,          // URL detection    #if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED    UIDataDetectorTypeAddress       = 1 << 2,          // Street address detection    UIDataDetectorTypeCalendarEvent = 1 << 3,          // Event detection#endif        UIDataDetectorTypeNone          = 0,               // No detection at all    UIDataDetectorTypeAll           = NSUIntegerMax    // All types};
看到这些,想必大家已经明白了一半了吧,最简单粗暴的办法是设置 dataDetectorTypes属性为UIDataDetectorTypeNone即可了。

之后再运行程序,点击 www.yesareno.com,就不会跳转了。

小技巧而已,随笔记录下。

0 0
原创粉丝点击