简体   繁体   中英

Phone call button in Swift 3 doesn't work

I need my app make a phone call when user taps a button. I found some tutorials and people there say use this code

let numberUrl = URL(string: "tel://12345678")!
UIApplication.shared.open(numberUrl, options: [:], completionHandler: nil)

But it just does not work. Was it change in Swift 3?

UPDATE

Try this code for Call. Code Works for all iOS versions

Swift 2.0

 let url = URL(string: "tel://123456789")!
 UIApplication.shared.openURL(url)

Swift 3.0

 let url = URL(string: "tel://123456789")!
 UIApplication.shared.open(url)

According to your comment, you want to deal with the deprecation warning.

if #available(iOS 10, *) {
    UIApplication.shared.open(numberUrl)
} else {
    UIApplication.shared.openURL(numberUrl)
}

try this way in your real device, simulator doesn't place a call.

let phoneUrl = "tel://45657657" //your url
      if let url = URL(string: phoneUrl){
           if #available(iOS 10.0, *) {
              UIApplication.shared.open(url, options: [:])
            } else {
              // Fallback on earlier versions
            }
       }

Try this...

let url:URL = URL(string: "tel:\(phone_number)")!
    let application:UIApplication = UIApplication.shared
    if (application.canOpenURL(url)) {
        if #available(iOS 10, *) {
            application.open(url)
        } else {
            application.openURL(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