Search code examples
iosswiftfunctionphone-call

Given a phone number, how do I make a phone call in iOS?


I have a 10 digit phone number held in a string "3133133313" that is downloaded from CloudKit.

I'm looking to use this number to place a call. Therefore, I have the code suggested by Apple.

    let call = detail.value(forKey: "Phone") as? String
    let url = URL(string: call!)!
        UIApplication.shared.open(url, options: [:], completionHandler: nil)

Unfortunately, this code does not work. What am I missing here that is probably blatantly obvious.


Solution

  • Thanks to Leo Dabus,

    I simply had to add the "tel://" aspect to each phone number. In this project, I decided to add this code snippet into my function rather than my CloudKit records.

    if let call = detail.value(forKey: "Phone") as? String,
        let url = URL(string: "tel://\(call)"),
        UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url)
    }