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) -> Swift.Void) {
if navigationAction.request.url?.scheme == "tel" {
UIApplication.shared.openURL(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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…