-
-
Save alexkafer/3b1186f93846cdaab77b1f7a44e337f0 to your computer and use it in GitHub Desktop.
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) | |
} | |
} |
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)
}
}
`
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?
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?
Try to add the country code before the number, so you get something like +88 048....
I found the solution. I've included the phone number as a string in command. Previously was an integer.
I've tried to add the country code, but it overflow the integer.
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 ^___^
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")
}
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
@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.
Awesome, tks !