![](/img/trans.png)
[英]Call number function error canOpenURL: failed for URL: “tel://0478733797” - error: “This app is not allowed to query for scheme tel”
[英]swift3 - canOpenURL: failed for URL tel
我想打个电话,这是我的代码:
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)
}
}
我正在使用 swift 3 来执行此操作,但出现此错误:
-canOpenURL: failed for URL: "tel://09178883828" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
有什么想法吗?
您的代码运行良好。 如果您使用模拟器,请在实际设备上运行它。 您无法在 Mac/MacBook 上模拟通话。
请查看 Apple 文档中的Simulator Hardware Actions 。
某些 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)
}
网址应该是:
if let urlMobile = NSURL(string: "tel:///076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {
let phonenumber = "076938483"
guard let url = URL(string: "tel://\(phonenumber )") else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
在 URL 生成的 else 部分,打印任何内容,如果打印出来,您需要检查电话号码的格式。
@IBAction func Call(_ sender: Any) {
let busPhone = "7355535586"
if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
@objc func callBtn() {
let userPhone = String((phoneNum.filter {!" \n\t\r".contains($0)}))
if let url = URL(string: "tel://\(phoneNum)"), UIApplication.shared.canOpenURL(url) {
DispatchQueue.main.async {
UIApplication.shared.open(url)
}
}
}
注意:当您使用真实设备而不是模拟器运行时,此代码可以工作。 并且不要忘记在 info.plist 中添加 LSApplicationQueriesSchemes。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.