wkdatdetectortypes = .all未在wkwebview中检测到任何数据,例如电话号码,地址等。这仅在使用javascript加载html字符串时发生。如果使用loadHTMLString完成,则将正确检测数据。任何人都可以在这方面帮助我。谢谢。
这是我的参考代码:
let config = WKWebViewConfiguration()
config.dataDetectorTypes = [WKDataDetectorTypes.all]
webview = WKWebView(frame: .zero, configuration: config)
webview.isUserInteractionEnabled = true
webview.backgroundColor = .white
webview.navigationDelegate = self
webview.uiDelegate = self
webview.allowsLinkPreview = true
webview.translatesAutoresizingMaskIntoConstraints = false
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("index.html") //index.html is in the same project file path.
webview.loadFileURL(html, allowingReadAccessTo:bundleURL)
let testString = "My contact number is: 1243233423"
///使用javascript加载html字符串。
let stringToBeLoaded = "var text=\"\(testString)\";showText(text); //showText() is a method in the index.html file to set the content.
webview.evaluateJavaScript(stringToBeLoaded, completionHandler: nil) //Doing this way is not detecting the phone number
//加载html字符串
webview.loadHTMLString(testString, baseURL: nil) //Doing this way is identifying the phone number and converting it to a clickable link from which I can call to this number.