Last active
January 31, 2022 21:40
-
-
Save alexkafer/3b1186f93846cdaab77b1f7a44e337f0 to your computer and use it in GitHub Desktop.
Simple function to prompt a phone call on iOS in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } }
`
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.