iOS应用中的Web视图内的手机链接

问题描述 投票:1回答:1

我有一个iOS应用程序,它有一个Web视图(使用WebKit),它有一些联系我们的信息,包括一个电话号码。在应用程序中,我希望我的用户能够单击锚标记,应用程序会询问客户端是否要拨打该号码。我在我的html中写了以下内容。

<meta name="format-detection" content="telephone=yes">

我也尝试了所有以下锚点结构,没有运气。

<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>

我必须单击并按住锚标记才能显示此对话框,但在Android中,所有您需要做的只是简单点击而不保留。这在iOS中是否可行,如果是这样,请告诉我们如何? what happen when I press and hold on the anchor tag

ios html5 safari swift4
1个回答
1
投票

测试一周后,为了找到该问题的解决方案,好吧。 这是解决方案。它与电话结构并不重要,因为它重写了我编写的代码,用于处理用户点击它后发生的事情(来自Web视图中的应用程序)。 在负责该类的类中(您的视图控制器)

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
© www.soinside.com 2019 - 2024. All rights reserved.