WKWebKit: no dataDetectorTypes parameters

In UIWebView it was pretty easy to add UIDataDetectorTypes to the view:

 myUIWebView.dataDetectorTypes = UIDataDetectorTypePhoneNumber; 

And so on. However, WKWebView does not seem to have a similar property. This link mentions that it went into the WKWebViewConfiguration property in myWebKitView.configuration , but the official documentation and the headers themselves do not reference dataDetectorTypes .

I am currently trying to port an application using UIWebView to WKWebView , and this application is currently being configured by user UIDataDetectorTypes . So, is there a way to implement this using the provided API, or will I have to write my own code to parse the HTML?

+6
source share
4 answers

This article has been updated to reflect changes in the API between iOS 8 beta versions. Starting with 8.0.1, WKWebView does not have the dataDetectorTypes property, without another comparable public API.

Until it is added back to the class, you will have to implement this yourself using the NSDataDetector or put up with the UIWebView .

+6
source

In fact, WKwebView does not have a dataDetectorTypes property. But in iOS 10 there is a WKWebViewConfiguration .

Try the following code snippet.

 WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; theConfiguration.dataDetectorTypes = WKDataDetectorTypeNone; WKWebView *webView = [[WKWebView alloc] initWithFrame:_someFrame configuration:theConfiguration]; 

This will only work with iOS10 onwards.

+5
source

The dataDetectorTypes property has been added to the WKWebViewConfiguration in iOS10.

Parameters: phone number, link, address, calendarEvent, tracking number, FlightNumber, lookupSuggestion and all.

+1
source

A simple workaround to support a phone number detector in WKWebView is to apply regular expression checking 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]; 
0
source

Source: https://habr.com/ru/post/975659/


All Articles