【问题标题】:Swift make a phone call from the WebViewSwift 从 WebView 拨打电话
【发布时间】:2015-07-09 00:35:55
【问题描述】:

我正在尝试通过点击WebView 拨打电话,但无法正常工作。

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if navigationType == UIWebViewNavigationType.LinkClicked {

        if (request.URL?.absoluteString!.rangeOfString("tel://") != nil) {

            var phone : String = request.URL!.absoluteString!

            println(phone)

            var url:NSURL? = NSURL(string: phone)
            UIApplication.sharedApplication().openURL(url!)

            return false

        } else {

            return true

        }
    }

    return true
}

提前致谢!

【问题讨论】:

    标签: swift ios8 ios-web-app


    【解决方案1】:

    这是未经测试的,但我认为你可以这样做:

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.LinkClicked {
            let application = UIApplication.sharedApplication()
            if let phoneURL = request.URL where (phoneURL.absoluteString!.rangeOfString("tel://") != nil) {
                if application.canOpenURL(phoneURL) {
                    application.openURL(phoneURL)
                    return false
                }
            }
        } 
        return true
    }
    

    您应该注意,这在模拟器上不起作用,因为application.canOpenURL(phoneURL) 将返回false。这仅适用于实际的 iPhone。

    【讨论】:

    • 谢谢,我在模拟器中测试,因为我很笨!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多