Hi, how do I make a button so that when it is pressed, it calls a phone number?
How to make a phone call with a button
Found an answer in SO : h ttps://stackoverflow.com/questions/27259824/calling-a-phone-number-in-swift
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
You should be able to use callNumber("7178881234") to make a call.
What Claude31 said plus…
There’s some things you should be aware of:
iOS has some non-obvious restrictions on the specific
URLs you can use. See the Phone Links section of Apple URL Scheme Reference for details.tel:
You shouldn’t build URLs via string interpolation but rather using
. This will take care of any escaping that might be necessary.URLComponents
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
I think i got something wrong when i did this? the app crashes as soon as i press the call button on my phone?
Code Block // // ViewController.swift // Diabell App // // Created by Richard Klug on 23/04/2021. // import UIKit import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } //Function Email Bottom Left @IBAction func Mail(_ sender: Any) { showMailComposer() } func showMailComposer() { guard MFMailComposeViewController.canSendMail() else{ return } let composer = MFMailComposeViewController() composer.mailComposeDelegate = self composer.setToRecipients(["richard.klug@diabell.se"]) composer.setSubject("Diabell App Help") composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false) present(composer, animated: true) } //Funktion Call Bottom Right @IBAction func callButtonClicked(_ sender: Any) { } private func callNumber(phoneNumber:String) { if let phoneCallURL = URL(string: "tel://\(+46706106310)") { let application:UIApplication = UIApplication.shared if (application.canOpenURL(phoneCallURL)) { application.open(phoneCallURL, options: [:], completionHandler: nil) } } } }
I need an answer for this too
Anyone figure out how to bypass the action sheet? Is that possible? @eskimo @ilovecats516