Swift 从 WebView 进行 phone 调用

Swift make a phone call from the WebView

我正在尝试通过点击 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
}

提前致谢!

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

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) 将 return false。这仅适用于实际的 iPhone.