swift3 - canOpenURL:URL tel失败

j j*_*nes 2 ios swift swift3

我想打电话,这是我的代码:

if let urlMobile = NSURL(string: "tel://076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {

                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(urlMobile as URL, options: [:], completionHandler: nil)
                }
                else {
                    UIApplication.shared.openURL(urlMobile as URL)
                }
            }
Run Code Online (Sandbox Code Playgroud)

我正在使用swift 3这样做,但我收到此错误:

-canOpenURL: failed for URL: "tel://09178883828" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
Run Code Online (Sandbox Code Playgroud)

有没有想过这样做?

Ole*_*ats 5

你的代码完美无缺.如果您使用的是模拟器,请在实际设备上运行它.您无法在Mac/MacBook上模拟通话.

请查看Apple文档中的模拟器硬件操作.


Man*_*jan 5

某些 LSApplicationQueriesScheme 不适用于模拟器。错误代码 -10814 适用于 kLSApplicationNotFoundErr。模拟器无法启动电话拨号盘。所以在 iPhone 设备上运行它。

这对我有用!

代码应该是

if let url = NSURL(string: "tel://\(yourNumber)"), UIApplication.shared.canOpenURL(url as URL) {
  UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)