Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch and call telephone number in WKWebView

I have a WKWebView that I want to ask to call a number when the number is selected. The contents of the web view contain the HTML anchor tag "tel:" and i am looking for a way to catch it. Which function is used to catch these tags?

like image 263
Tamarisk Avatar asked Nov 11 '15 02:11

Tamarisk


People also ask

Is WKWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated. New apps containing these frameworks are no longer accepted by the App Store.

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.

Is WKWebView the same as Safari?

WKWebView. WKWebView was introduced in iOS 8 allowing app developers to implement a web browsing interface similar to that of mobile Safari. This is due, in part, to the fact that WKWebView uses the Nitro Javascript engine, the same engine used by mobile Safari.

What is Mobile Safari UI WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

Set the webView's navigationDelegate property and implement the following function of the delegate (WKNavigationDelegate)

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

Since iOS 10, you can also set dataDetectorTypes to .phoneNumber on your WKWebViewConfiguration. All detected phone numbers will transformed to contain links around the phone number and thus the above function will be fired with a URL with a "tel" scheme when tapping on a phone number.

configuration.dataDetectorTypes = .phoneNumber
like image 109
Carien van Zyl Avatar answered Sep 28 '22 10:09

Carien van Zyl


Fixed to make phone call in WKWebView by using the "decidePolicyFor navigationAction" delegate method:

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

When setting up your web view set the data detector type on its configuration:

if #available(iOS 10.0, *) {
    webView.configuration.dataDetectorTypes = .phoneNumber
}
like image 21
user8879582 Avatar answered Sep 28 '22 08:09

user8879582