简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM