2

Swift 4 iOS 11.4

I'm trying to learn how to use the WKWebView but I'm having an issue where any phone-number links won't actually display the popup to call that number.

I followed this answer and the error that prints is listed below. The website I'm using is a wix site that uses the quickactionbar (I know this uses tel:xxxxx so it should be working since it works on safari) Any idea why it's doing this?

Edit: Also, I'm not entirely sure what to search for in order to get the answer I need. If you know exactly what to search to find the answer to this, I'll be more than happy with that as well.

It recognizes it's a phone number if I long press but won't do anything if I just tap it

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


}

The error:

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)"}}
Keithers
  • 354
  • 6
  • 23

1 Answers1

8

you can also set dataDetectorTypes to .phoneNumber on your WKWebViewConfiguration. All detected phone numbers will transformed to contain links around the phone number.

configuration.dataDetectorTypes = .phoneNumber

Use this delagate func-

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)
}
}
shivi_shub
  • 1,018
  • 1
  • 7
  • 15
  • So I should probably do some reading into dataDetectorTypes correct? Why wouldn't this work out of box if the documentation explicitly states it will recognize the phone number without any modification? – Keithers Jul 23 '18 at 08:44
  • https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview#declarations This documentation is useless. Do you know of anywhere else that might be helpful for this sort of thing? – Keithers Jul 23 '18 at 08:54
  • You can try on www.appcoda.com – shivi_shub Jul 23 '18 at 09:13