如何使用 Swift 在 iOS 10 中拨打电话? [英] How to make phone call in iOS 10 using Swift?

查看:50
本文介绍了如何使用 Swift 在 iOS 10 中拨打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序能够在单击按钮时呼叫某个号码.我试过用谷歌搜索它,但到目前为止似乎没有一个适用于 iOS 10 的(openURL 不见了).有人可以为我举个例子吗?例如:

I want my app to be able to call a certain number when a button is clicked. I've tried to google it but there doesn't seem to have one for iOS 10 so far (where openURL is gone). Can someone put an example for me on how to do so? For instance like:

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

推荐答案

你可以这样调用:

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

对于Swift 3+,你可以使用like

For Swift 3+, you can use like

guard let number = URL(string: "tel://" + number) else { return }
UIApplication.shared.open(number)

UIApplication.shared.open(number, options: [:], completionHandler: nil)

确保您已清除电话号码字符串以删除 ()-space 的任何实例.

Make sure you've scrubbed your phone number string to remove any instances of (, ), -, or space.

这篇关于如何使用 Swift 在 iOS 10 中拨打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆