Marco Dinatsoli Marco Dinatsoli - 2 months ago 6
iOS Question

where is the wrong in my function to make a call

this is my code to make a phone call

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)
}


i am getting this error in the log

ERROR: There is no registered handler for URL scheme tel


hint: i already check this question, but the solution didn't work with me
Calling a phone number in swift

Answer

the correct url scheme is something like tel:PHONE_NUMBER (without the slashes you included). you should also check if the application CAN open the url you provide by calling UIApplication.sharedApplication.canOpenURL()...

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