如何使用WKWebView点按电话号码

时间:2018-07-21 21:07:26

标签: ios swift wkwebview

雨燕4 iOS 11.4

我正在尝试学习如何使用WKWebView,但是我遇到了一个问题,即任何电话号码链接实际上都不会显示用于呼叫该号码的弹出窗口。

我遵循了this的回答,下面列出了打印错误。我正在使用的网站是一个使用quickactionbar的wix网站(我知道它使用tel:xxxxx,因为它可以在safari中工作,所以应该可以正常工作)。为什么要这样做?

编辑: 另外,我不确定要搜索什么才能获得所需的答案。如果您确切地知道要搜索什么才能找到答案,我也会对此感到满意。

如果我长按它会识别出它是一个电话号码,但是只要我轻按它就不会做任何事情

import Foundation
import WebKit

class WebKitViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

var webView: WKWebView!

    @IBOutlet weak var backButton: UIBarButtonItem!

    @IBAction func backButtonAction(_ sender: UIBarButtonItem) {
        if self.webView.canGoBack {
            self.webView.goBack()
        }
    }
    override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.navigationDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = URL(string: "Link")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)

}

//    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//        backButton.isEnabled = webView.canGoBack
//    }


//Code from answer on StackOverflow
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        print("didStartProvisionalNavigation: \(navigation)")
    }

    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation) {
        print("didReceiveServerRedirectForProvisionalNavigation: \(navigation)")
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation, withError error: Error) {
        print("didFailProvisionalNavigation: \(navigation), error: \(error)")
    }

    func webView(_ webView: WKWebView, didFinishLoading navigation: WKNavigation) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
        print("didFinishLoadingNavigation: \(navigation)")
    }


    func _webViewWebProcessDidCrash(_ webView: WKWebView) {
        print("WebContent process crashed; reloading")
    }

    func webView(_ webView: WKWebView,createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?{
        //if <a> tag does not contain attribute or has _blank then navigationAction.targetFrame will return nil
        if let trgFrm = navigationAction.targetFrame {

            if(!trgFrm.isMainFrame){
                UIApplication.shared.isNetworkActivityIndicatorVisible = true
                self.webView.load(navigationAction.request)
            }
        }



        return nil
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){

        UIApplication.shared.isNetworkActivityIndicatorVisible = false
        print("didFinish: \(String(describing: self.webView.url)); stillLoading:\(self.webView.isLoading ? "NO" : "YES")")

    }



    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (_: WKNavigationResponsePolicy) -> Void) {
        print("decidePolicyForNavigationResponse")
        decisionHandler(.allow)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void) {

        decisionHandler(.allow)
    }


}

错误:

didFailProvisionalNavigation: <WKNavigation: 0x101865bd0>, error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={_WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x101856b80>, NSErrorFailingURLStringKey=tel:+12199874536, NSErrorFailingURLKey=tel:+12199874536, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x1018641a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}

1 个答案:

答案 0 :(得分:4)

您还可以在WKWebViewConfiguration上将dataDetectorTypes设置为.phoneNumber。检测到的所有电话号码都会转换为包含电话号码周围的链接。

configuration.dataDetectorTypes = .phoneNumber

使用该脱盐功能-

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
if navigationAction.request.url?.scheme == "tel" {
    UIApplication.shared.openURL(navigationAction.request.url!)
    decisionHandler(.cancel)
} else {
    decisionHandler(.allow)
}
}