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?
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.
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.
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.
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.
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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With