-1

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nslllava
  • 589
  • 2
  • 9
  • 19

4 Answers4

5

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)
Nimisha Ranipa
  • 8,304
  • 2
  • 14
  • 29
1

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)
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
0

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
            }
       }
john afordis
  • 317
  • 3
  • 15
-1

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)
        }
    }
Rishu Gupta
  • 43
  • 1
  • 14
  • Yes, check the condition that checks for if application can open url or not – Rishu Gupta Nov 17 '17 at 12:40
  • Please stop upvoting each other's stuff. And please only post useful answers. That's all I'm saying. Be honest, be fair, and post good content that has not already been posted a thousand times. – Eric Aya Nov 17 '17 at 12:45
  • Yes, @KiranSarvaiya. You and Rishu should flag as duplicate *instead* of posting the same stuff again. – Eric Aya Nov 17 '17 at 12:49