2015-11-05 46 views
2

这是我的代码打个电话什么是错误的,我的功能来拨打电话

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 

暗示:我已经检查这个问题,但解决方案并没有跟我 Calling a phone number in swift

+0

您是在真实设备上测试吗?您无法在模拟器中拨打电话。 – rmaddy

+0

顺便说一句 - 你应该检查'openURL'的结果。 – rmaddy

+0

@rmaddy哦,我真的不能打开打电话的控制器? –

回答

2

工作正确的URL方案是一样的东西 tel:PHONE_NUMBER(不包括你的斜杠)。你也应该检查应用程序可以通过调用UIApplication.sharedApplication.canOpenURL()打开您提供的网址...

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

有没有参考? ? –

+0

sure:https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html#//apple_ref/doc/uid/TP40007899-CH6-SW1 –

+0

我得到了同样的错误信息 –

1

每苹果文档https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html

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

+0

测试,我可以安装应用程序制作仿真器上的电话? –

+0

@MarcoDinatsoli不,你将需要一个真正的设备上运行此。 –

+0

感谢您的希望,加上一个 –