我打电话的功能出了什么问题



这是我打电话给的代码

let phoneCall = phoneNumberTF.text
        if let phoneCall = phoneCall {
            let url = NSURL(string: "tel://(phoneCall)")
            UIApplication.sharedApplication().openURL(url!)
        }else {
            let alert = UIAlertController(title: "Error", message: "not correct phone", preferredStyle: .Alert)
            let action = UIAlertAction(title: "Retry", style: .Default, handler: nil)
            alert.addAction(action)
        }

我在日志中得到这个错误

ERROR: There is no registered handler for URL scheme tel

提示:我已经检查了这个问题,但解决方案对我不起作用在swift 中呼叫电话号码

正确的url方案类似于tel:PHONE_NUMBER(没有包含斜杠)。您还应该检查应用程序是否可以通过调用UIApplication.sharedApplication.canOpenURL()打开您提供的url。。。

let phoneNumber = "123-456-789"
let phoneURL = NSURL(string: "tel:(phoneNumber)")!
if UIApplication.sharedApplication().canOpenURL(phoneURL) {
  UIApplication.sharedApplication().openURL(phoneURL)
}

根据Apple文档https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html

正确的格式是tel:。所以,首先要改变它,然后你应该在做之前检查UIApplication.sharedApplication.canOpenURL()。如果你在模拟器上运行,它将不起作用。

最新更新