Skip to content

Instantly share code, notes, and snippets.

@alexkafer
Last active January 31, 2022 21:40
Show Gist options
  • Save alexkafer/3b1186f93846cdaab77b1f7a44e337f0 to your computer and use it in GitHub Desktop.
Save alexkafer/3b1186f93846cdaab77b1f7a44e337f0 to your computer and use it in GitHub Desktop.
Simple function to prompt a phone call on iOS in Swift
func makePhoneCall(phoneNumber: String) {
if let phoneURL = NSURL(string: ("tel://" + phoneNumber!)) {
let alert = UIAlertController(title: ("Call " + phoneNumber! + "?"), message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Call", style: .Default, handler: { (action) in
UIApplication.sharedApplication().openURL(phoneURL)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
@wonder2011
Copy link

Awesome, tks !

@SaladDays831
Copy link

SaladDays831 commented Apr 13, 2019

Small update for Swift 5.0 :)

`
func makePhoneCall(phoneNumber: String) {

    if let phoneURL = NSURL(string: ("tel://" + phoneNumber)) {

        let alert = UIAlertController(title: ("Call " + phoneNumber + "?"), message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Call", style: .default, handler: { (action) in
            UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

`

@lplazas
Copy link

lplazas commented May 17, 2019

Thanks for the update @SaladDays831. I am getting a double alert as iOS natively displays another confirmation dialog, do you know any way to get rid of it?

@Machiuka
Copy link

In my country all phone numbers starts with zero. And Swift omit that leading zero, therefore I'm unable to place a call. What could be the solution for that?

@lplazas
Copy link

lplazas commented May 26, 2019

Try to add the country code before the number, so you get something like +88 048....

@Machiuka
Copy link

I found the solution. I've included the phone number as a string in command. Previously was an integer.

@Machiuka
Copy link

I've tried to add the country code, but it overflow the integer.

@bugrym
Copy link

bugrym commented Jul 18, 2019

Small update for Swift 5.0 :)

`
func makePhoneCall(phoneNumber: String) {

    if let phoneURL = NSURL(string: ("tel://" + phoneNumber)) {

        let alert = UIAlertController(title: ("Call " + phoneNumber + "?"), message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Call", style: .default, handler: { (action) in
            UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

`

Perfect, thanks ^___^

@chitu98
Copy link

chitu98 commented Aug 7, 2019

Small update for Swift 5.0 :)

`
func makePhoneCall(phoneNumber: String) {

    if let phoneURL = NSURL(string: ("tel://" + phoneNumber)) {

        let alert = UIAlertController(title: ("Call " + phoneNumber + "?"), message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Call", style: .default, handler: { (action) in
            UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

`

Working like charm.
just for someone who is a beginner and not able to call.
pass the function in button, make an action connection from storyboard to view controller.

@IBAction func btnCall(_ sender: UIButton) {
makePhoneCall(phoneNumber: "1234567890")
}

Copy link

ghost commented Jun 5, 2020

Small update for Swift 5.0 :)
`
func makePhoneCall(phoneNumber: String) {

    if let phoneURL = NSURL(string: ("tel://" + phoneNumber)) {

        let alert = UIAlertController(title: ("Call " + phoneNumber + "?"), message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Call", style: .default, handler: { (action) in
            UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

`

Working like charm.
just for someone who is a beginner and not able to call.
pass the function in button, make an action connection from storyboard to view controller.

@IBAction func btnCall(_ sender: UIButton) {
makePhoneCall(phoneNumber: "1234567890")
}

when entering this line of code > > makePhoneCall(phoneNumber: "1234567890"), 'expected parameter type' error message occurs, new to coding and unsure what to do, Regards

@unferna
Copy link

unferna commented Jan 31, 2022

@lplazas just call the UIApplication.shared.open(_:) it will run the native phone call alert. That could work if your just want an alert with only one number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment