如何使用Swift在iOS 10中拨打电话?

我希望我的应用能够在单击button时调用某个号码。 我试图谷歌,但目前似乎没有一个iOS 10(openURL已经消失)。 有人能为我做一个例子吗? 比如像:

@IBAction func callPoliceButton(_ sender: UIButton) { // Call the local Police department } 

你可以这样打电话:

  if let url = URL(string: "tel://\(number)") { UIApplication.shared.openURL(url) } 

对于Swift 3,你可以使用like

 guard let number = URL(string: "tel://" + number) else { return } UIApplication.shared.open(number) OR UIApplication.shared.open(number, options: [:], completionHandler: nil) 

确保你已经清理你的电话号码string,以删除()-space任何实例。

更新了Swift 3:

使用下面简单的代码行,如果你想打个电话:

//function定义:

 func makeAPhoneCall() { let url: NSURL = URL(string: "TEL://1234567890")! as NSURL UIApplication.shared.open(url as URL, options: [:], completionHandler: nil) } 

//函数调用:[在代码中的任何地方使用]

 self.makeAPhoneCall() 

注意:请在真实设备上运行应用程序,因为它在模拟器上不起作用。

 if let phoneCallURL:URL = URL(string: "tel:\(strPhoneNumber)") { let application:UIApplication = UIApplication.shared if (application.canOpenURL(phoneCallURL)) { let alertController = UIAlertController(title: "MyApp", message: "Are you sure you want to call \n\(self.strPhoneNumber)?", preferredStyle: .alert) let yesPressed = UIAlertAction(title: "Yes", style: .default, handler: { (action) in application.openURL(phoneCallURL) }) let noPressed = UIAlertAction(title: "No", style: .default, handler: { (action) in }) alertController.addAction(yesPressed) alertController.addAction(noPressed) present(alertController, animated: true, completion: nil) } }