xcode WKWebKit:没有 dataDetectorTypes 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25977764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:39:41  来源:igfitidea点击:

WKWebKit: No dataDetectorTypes parameter

iosobjective-cxcodeuiwebviewwebkit

提问by Steve Richey

In UIWebView, it was fairly easy to add UIDataDetectorTypesto a view:

在 中UIWebView,添加UIDataDetectorTypes到视图中相当容易:

myUIWebView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;

And so on. However, WKWebViewdoes not seem to have a similar property. Thisreference mentions that it has moved to the WKWebViewConfigurationproperty at myWebKitView.configuration, but both the official documentationand the headers themselves make no reference to dataDetectorTypes.

等等。但是,WKWebView似乎没有类似的属性。参考文献提到它已移至WKWebViewConfigurationat 属性myWebKitView.configuration,但官方文档和标题本身均未提及dataDetectorTypes.

I'm currently trying to migrate an app from using UIWebViewto WKWebView, and this app currently has user-configurable UIDataDetectorTypes. So, is there any way to implement this using the provided API, or would I have to write my own code to parse the HTML?

我目前正在尝试将应用程序从 using 迁移UIWebViewWKWebView,并且该应用程序当前具有用户可配置的UIDataDetectorTypes。那么,有没有办法使用提供的 API 来实现这一点,或者我是否必须编写自己的代码来解析 HTML?

采纳答案by mattt

The cited article has been updated to reflect changes in the API between iOS 8 betas. As of 8.0.1, there is no dataDetectorTypesproperty on WKWebView, with no other comparable public API.

引用的文章已更新,以反映 iOS 8 测试版之间 API 的变化。从 8.0.1 开始,没有dataDetectorTypes关于 的属性WKWebView,也没有其他类似的公共 API。

Until it's added back into the class, you'd have to implement this yourself with NSDataDetector, or resign yourself to using UIWebView.

在它被重新添加到类中之前,您必须自己使用NSDataDetector实现它,或者让自己使用UIWebView.

回答by arango_86

Actually WKwebView doesn't have a dataDetectorTypesproperty. But In iOS 10 WKWebViewConfigurationhas.

实际上 WKwebView 没有dataDetectorTypes属性。但是在 iOS 10 中WKWebViewConfiguration有。

Try the following code snippet.

试试下面的代码片段。

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
theConfiguration.dataDetectorTypes = WKDataDetectorTypeNone;

WKWebView *webView = [[WKWebView alloc] initWithFrame:_someFrame configuration:theConfiguration];

This will work only from iOS10 onwards.

这仅适用于 iOS10 以后。

回答by Alpana

WKWebViewdata detectors can be set in WKWebViewConfiguration. Swift version:

WKWebView数据检测器可以设置在WKWebViewConfiguration. 迅捷版:

let webViewCofig = WKWebViewConfiguration()
webViewCofig.dataDetectorTypes = [.address]
webView = WKWebView(frame: view.frame, configuration: webViewCofig)

Valid options are phoneNumber, link, address, calendarEvent, trackingNumber, flightNumber, lookupSuggestion and all. Pass empty []to set to none.

有效选项是 phoneNumber、link、address、calendarEvent、 trackingNumber、flightNumber、lookupSuggestion 等。通过空[]设置为无。

回答by Carien van Zyl

The property dataDetectorTypes has been added to WKWebViewConfiguration in iOS10.

在 iOS10 中,属性 dataDetectorTypes 已添加到 WKWebViewConfiguration。

The options are phoneNumber, link, address, calendarEvent, trackingNumber, flightNumber, lookupSuggestion and all.

选项包括 phoneNumber、link、address、calendarEvent、 trackingNumber、flightNumber、lookupSuggestion 等等。

回答by Abhijit

A simple workaround for supporting phone number detector in WKWebView would be to apply regex checker in javascriptvia WKUserScript

在 WKWebView 中支持电话号码检测器的一个简单解决方法是通过WKUserScript在 javascript 中应用正则表达式检查器

NSString *jScript = @"document.getElementById(\"main\").innerHTML.replace(/[\+\d]{1}[\d]{2,4}[\s,][\d\s-\(\),]{7,}/g, \"<a href=\"tel:$&\">$&</a>\")";

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;

wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];