Jon
Jon

Reputation: 85

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.

Upvotes: 0

Views: 8027

Answers (1)

Jon
Jon

Reputation: 85

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

Upvotes: 7

Related Questions