代码之家  ›  专栏  ›  技术社区  ›  Keithers

如何使用WKWebView轻触电话号码

  •  1
  • Keithers  · 技术社区  · 6 年前

    斯威夫特4 iOS 11.4版

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

    我跟着 this 答案和打印的错误如下所示。我使用的网站是一个wix网站,使用quickactionbar(我知道它使用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 回复  |  直到 6 年前
        1
  •  5
  •   shivi_shub    6 年前

    您还可以在wkwebviewconfiguration中将datadetectortypes设置为.phonenumber。所有检测到的电话号码将被转换为包含电话号码周围的链接。

    configuration.dataDetectorTypes = .phoneNumber
    

    使用此delagate函数-

    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)
    }
    }