我正试图从click onWebView打电话,但没有成功。

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
}

提前谢谢!

最佳答案

这是未经测试的,但我认为您可以做如下操作:

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。

关于swift - Swift从WebView拨打电话,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31306206/

10-09 13:23