WKWebKit: No dataDetectorTypes parameter
Asked Answered
H

6

17

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?

Hughey answered 22/9, 2014 at 15:26 Comment(0)
M
11

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.

Mackinnon answered 2/10, 2014 at 2:1 Comment(3)
Thanks for responding. Do you have any thoughts on how such a property would be implemented? I was going to use evaluateJavaScript:completionHandler: to get the HTML of the web page on load, and then apply an NSDataDetector to the text, but I'm not sure if that's an optimal method.Hughey
We need to write a WKWebView extension for this and make it available on GitHub.Soutache
In iOS10 WKWebViewConfiguration has property "dataDetectorTypes"Educationist
E
26

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.

Educationist answered 20/10, 2016 at 8:26 Comment(3)
this is working fine we take WKWebView using code. .but what if we take wkwebview in xib directly ? not found any method to set configuration.Graduation
@Graduation there is data detector settings in xib.Circuity
@ParagBafna . Thanks got itGraduation
M
11

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.

Mackinnon answered 2/10, 2014 at 2:1 Comment(3)
Thanks for responding. Do you have any thoughts on how such a property would be implemented? I was going to use evaluateJavaScript:completionHandler: to get the HTML of the web page on load, and then apply an NSDataDetector to the text, but I'm not sure if that's an optimal method.Hughey
We need to write a WKWebView extension for this and make it available on GitHub.Soutache
In iOS10 WKWebViewConfiguration has property "dataDetectorTypes"Educationist
S
5

You can find in the storyboard attribute inspector

Screenshot

Shaeshaef answered 5/3, 2021 at 13:49 Comment(0)
T
3

WKWebView data detectors can be set in WKWebViewConfiguration. Swift version:

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.

Tenotomy answered 17/4, 2020 at 12:48 Comment(0)
H
1

The property dataDetectorTypes has been added to WKWebViewConfiguration in iOS10.

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

Hekker answered 4/10, 2016 at 14:50 Comment(0)
T
0

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];
Trite answered 10/5, 2016 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.