Why is WKWebView not opening a telephone link in ios 9?

Viewed 3267

I have a WKWebView that loads a webpage with some telephone links on the webpage.

Currently i have this code to handle clicks to those links.

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.request.url?.scheme == "tel" {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(navigationAction.request.url!)
        }
        decisionHandler(.cancel)
        return
    }
    decisionHandler(.allow)
}

This works fine on any device with ios 10 installed, i am prompted with an alert box asking to either cancel or make a call. but on ios 9 devices the telephone app screen flashes (no prompt) and it nothing happens after.

2 Answers

Swift 4:

 webView.navigationDelegate = self
 webView.configuration.dataDetectorTypes = [.link, .phoneNumber]

extension PDFWebViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let requestUrl = navigationAction.request.url, requestUrl.scheme == "tel" {
            UIApplication.shared.open(requestUrl, options: [:], completionHandler: nil)
            decisionHandler(.cancel)

        } else {
            decisionHandler(.allow)
        }
    }

}
Related