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?
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