How to make a phone call with a button

Hi, how do I make a button so that when it is pressed, it calls a phone number?

Replies

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

    tel:
    URLs you can use. See the Phone Links section of Apple URL Scheme Reference for details.
  • You shouldn’t build URLs via string interpolation but rather using

    URLComponents
    . This will take care of any escaping that might be necessary.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • Is it also possible to change the phone image or its colour ? iOS 13 onwards the call option is being displayed as an action sheet so we are looking for ways to show a custom phone icon or change the colour of the existing phone icon. is that possible to do ?

Add a Comment
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)
      }
     }
    }
}


  • @ Richard_Klug where does it crash ? did you read eskimo answer on how to form url ?

Add a Comment

I need an answer for this too

Anyone figure out how to bypass the action sheet? Is that possible? @eskimo @ilovecats516