SwiftDeveloper
SwiftDeveloper

Reputation: 7370

Call phone action don't work in swift

Hello I've button action for call number , but when I used it don't call and nothing shows.

My codes under below.

  @IBAction func callPhone(sender: AnyObject) {
        UIApplication.shared().canOpenURL((NSURL(string: "tel://1234567890")! as URL))
    }

Thank You !

Upvotes: 1

Views: 3836

Answers (6)

Michael Sacks
Michael Sacks

Reputation: 917

In your Info.plist, make sure you have enabled calling

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>tel</string>
    <string>sms</string>
</array>

The sms line may be optional, depending on your use case.

Also: Consider NOT calling canOpenUrl if you are calling that before.

Make sure your phone number string is formatted like this: "tel://+12813308004"

Upvotes: 0

SwiftDeveloper
SwiftDeveloper

Reputation: 7370

Latest Xcode , Latest Swift working codes.

use telprompt:// not tel

let myphone = "+134345345345"

 if let phone = URL(string:"telprompt://\(myphone)"), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }

Upvotes: 2

alessioarsuffi
alessioarsuffi

Reputation: 561

please note that:

  • tel:// try to call direct the phone number;
  • telprompt:// shows you an alert to confirm call

as of iOS 10 openUrl is deprecated;

@available(iOS, introduced: 2.0, deprecated: 10.0, message: "Please use openURL:options:completionHandler: instead") open func openURL(_ url: URL) -> Bool

so i advice to use this code block to support also iOS < 9:

if #available(iOS 10, *) {
    UIApplication.shared.open(yourURL)

    // if you need completionHandler:
    //UIApplication.shared.open(yourURL, completionHandler: { (aBool) in })

    // if you need options too:
    //UIApplication.shared.open(yourURL, options: [:], completionHandler: { (aBool) in })

} else {
    UIApplication.shared.openURL(number)
}

Upvotes: 1

user6375148
user6375148

Reputation:

Proper Swift 3.0 Code

    if let url = URL(string: "tel://\(phoneNumber)") {
      UIApplication.shared().open(url, options: [:], completionHandler: nil)
    }

In Swift 3.0 NSURL have changed to URL. And sharedApplciation changed to shared. Also OpenURL changed to open, they have added a bunch other parameters to the openmethod, you can pass empty dictionary in options and nil in the completionHandler.

Upvotes: 12

Tejveer Sambariya
Tejveer Sambariya

Reputation: 271

Try this answer.

 @IBAction func callPhone(sender: AnyObject) {

            if let url = NSURL(string: "tel://9069118117") {

            UIApplication.sharedApplication().openURL(url)

       }
    }

Upvotes: 1

Nimit Parekh
Nimit Parekh

Reputation: 16864

Please try following code it's use to solve your problem.

if let url = NSURL(string: "tel://\(1234567890)") {
  UIApplication.sharedApplication().openURL(url)
}

Upvotes: 1

Related Questions