WKWebKit: No dataDetectorTypes parameter

◇◆丶佛笑我妖孽 提交于 2019-12-04 17:57:46

问题


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

myUIWebView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;

And so on. However, WKWebView does not seem to have a similar property. This reference mentions that it has moved to the WKWebViewConfiguration property at myWebKitView.configuration, but both the official documentation and the headers themselves make no reference to dataDetectorTypes.

I'm currently trying to migrate an app from using UIWebView to 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?


回答1:


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

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




回答2:


Actually WKwebView doesn't have a dataDetectorTypes property. But In iOS 10 WKWebViewConfiguration has.

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.




回答3:


The property dataDetectorTypes has been added to WKWebViewConfiguration in iOS10.

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




回答4:


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

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];


来源:https://stackoverflow.com/questions/25977764/wkwebkit-no-datadetectortypes-parameter

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