在WKWebView中接听并拨打电话号码 [英] Catch and call telephone number in WKWebView
问题描述
我有一个WKWebView,我希望在选择该号码时要求呼叫该号码. Web视图的内容包含HTML锚标记"tel:",我正在寻找一种捕获它的方法.哪个功能可以捕获这些标签?
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?
推荐答案
设置webView的navigationDelegate
属性并实现委托的以下功能( WKNavigationDelegate )
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)
}
}
从iOS 10开始,您还可以在WKWebViewConfiguration
上将dataDetectorTypes
设置为.phoneNumber
.所有检测到的电话号码都将转换为包含电话号码周围的链接,因此,在点击电话号码时,将使用具有"tel"方案的URL触发上述功能.
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
这篇关于在WKWebView中接听并拨打电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!