6

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.
4

0 回答 0