gpt4 book ai didi

ios - iOS 应用程序中 WebView 内的电话链接

转载 作者:行者123 更新时间:2023-11-29 11:38:29 25 4
gpt4 key购买 nike

我有一个 iOS 应用程序,它有一个 WebView (使用 WebKit),其中有一些联系我们的信息,包括电话号码。在应用程序中,我希望我的用户能够单击 anchor 标记,然后应用程序会询问客户是否要调用该号码。我在 html 的头部写了以下内容。

<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 中可行吗?如果可以,请告诉我怎么做? what happen when I press and hold on the anchor tag

最佳答案

好的,经过一个多星期的测试以找到该问题的解决方案。
这是解决方案。电话结构并不重要,因为重要的是我编写的代码来处理用户点击它后发生的事情(从 WebView 中的应用程序)。
在负责的类中(您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com