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

标签 ios html safari swift4

我有一个 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 中可行吗?如果可以,请告知如何实现? what happen when I press and hold on the anchor tag

最佳答案

好的,经过一周多的测试,找到了该问题的解决方案。
这是解决方案。手机结构并不重要,重要的是我编写的用于处理用户单击它后发生的情况的代码(从 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/

相关文章:

javascript - HTML5 Canvas : How do I merge 2 canvas into 1 (of which 1 is draggable)

javascript - 为什么这个 Javascript 语法是非法的?

ios - 在 NSArray 中搜索值的范围

javascript - CSS Textsize 基于容器宽度?

html - 在新选项卡或窗口中打开链接

html - 选择内部 flex 元素时,文本选择背景在 Safari 中变得不可见

css - Safari 表格单元格大小

ios - For 循环中的 ObjC/ARC ByRef

ios - 选择部分中的最后一行后显示的分隔线

iphone - 如何删除 box2d Fixture