我有一个 iOS 应用程序,它有一个 WebView (使用 WebKit),其中包含一些联系我们的信息,包括电话号码。在应用程序中,我希望我的用户能够单击 anchor 标记,并且应用程序会询问客户是否要调用该号码。我在 html 的 head 中写了以下内容。
<meta name="format-detection" content="telephone=yes">
我还尝试了以下所有 anchor 结构,但没有成功。
<a href="tel:+x-xxx-xxx-xxx">+x-xxx-xxx-xxx</a>
<a href="tel:xxx-xxx-xxx">xxx-xxx-xxx</a>
<a href="tel:(xxx) xxx-xxx">(xxx) xxx-xxx</a>
<a href="tel:(xxx)-xxx-xxx">(xxx)-xxx-xxx</a>
我必须单击并按住 anchor 标记才能显示此对话框,但在 Android 中,您只需单击一下即可,无需按住。这在 iOS 中可行吗?如果可以,请告知如何实现?
最佳答案
好的,经过一周多的测试,找到了该问题的解决方案。
这是解决方案。手机结构并不重要,重要的是我编写的用于处理用户单击它后发生的情况的代码(从 Web View 内的应用程序)。
在负责该操作的类中(您的 View Controller )
extension ViewController: WKNavigationDelegate, WKUIDelegate {
// handle it here
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// print(navigationAction.request.url!)
// if the URL have tel in it then use the iOS to open it not the webkit
if (navigationAction.request.url!.absoluteString.contains("tel")) {
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
} else if (!(navigationAction.request.url!.absoluteString.contains(MAINURLNOTTOGOOUTSIDE))) {
// if the URL is something outside of the one specified in that string "MAINURLNOTTOGOOUTSIDE", open it using the iOS not the webkit
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let app = UIApplication.shared
if (navigationAction.targetFrame == nil) {
if (navigationAction.request.url!.scheme?.contains("tel"))! {
if (app.canOpenURL(navigationAction.request.url!)) {
app.openURL(navigationAction.request.url!)
}
}
}
return nil
}
// handle if failed to connect to the server
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
var message = ""
var title = ""
var button = ""
if (Locale.preferredLanguages[0].contains("ar")) {
title = "خطأ"
message = "تعذر الوصول الى الخادم الرجاء المحاولة في وقت لاحق"
button = "حسنا"
} else {
title = "Error"
message = "Unable to access server Please try again later"
button = "OK!"
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: button, style: .default, handler: nil))
}
// handle if failed to connect to the server
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
var message = ""
var title = ""
var button = ""
if (Locale.preferredLanguages[0].contains("ar")) {
title = "خطأ"
message = "تعذر الوصول الى الخادم الرجاء المحاولة في وقت لاحق"
button = "حسنا"
} else {
title = "Error"
message = "Unable to access server Please try again later"
button = "OK!"
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: button, style: .default, handler: nil))
}
}
也不要忘记在 viewDidLoad 方法中包含以下内容
self.WebView.navigationDelegate = self
关于ios - iOS 应用程序中 WebView 内的电话链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47859113/