[英]swift3 - canOpenURL: failed for URL tel
I want to intent a call , this is my code :我想打个电话,这是我的代码:
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)
}
}
I'm using swift 3 to do so but I get this error:我正在使用 swift 3 来执行此操作,但出现此错误:
-canOpenURL: failed for URL: "tel://09178883828" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
any idea to do so ?有什么想法吗?
Your code works perfectly fine.您的代码运行良好。 Run it on an actual device if your using Simulator.
如果您使用模拟器,请在实际设备上运行它。 You can't simulate a call on a Mac/MacBook.
您无法在 Mac/MacBook 上模拟通话。
Please have a look at Simulator Hardware Actions in Apple documentation.请查看 Apple 文档中的Simulator Hardware Actions 。
Some LSApplicationQueriesScheme do not work on Simulator.某些 LSApplicationQueriesScheme 在模拟器上不起作用。 Error Code -10814 is for kLSApplicationNotFoundErr.
错误代码 -10814 用于 kLSApplicationNotFoundErr。 Simulator can't launch Dial Pad for Telephone.
模拟器无法启动电话拨号盘。 So run it on iPhone device.
所以在 iPhone 设备上运行它。
This worked for me!!!这对我有用!!!
Code Should be代码应该是
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)
}
In the else part of the URL generation, print anything, if it is getting printed, you need to check the format of the phone number.在 URL 生成的 else 部分,打印任何内容,如果打印出来,您需要检查电话号码的格式。
@IBAction func Call(_ sender: Any) { @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)
}
}
}
Note : This code can work when you run with the real device not simulator.注意:当您使用真实设备而不是模拟器运行时,此代码可以工作。 And don't forget add LSApplicationQueriesSchemes in your info.plist.
并且不要忘记在 info.plist 中添加 LSApplicationQueriesSchemes。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.