How to make phone call in iOS 10 using Swift?

user3175707 picture user3175707 · Oct 17, 2016 · Viewed 95k times · Source

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
}

Answer

Parth Adroja picture Parth Adroja · Oct 17, 2016

You can call like this:

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

For Swift 3+, you can use like

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

OR

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

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