The URL scheme is an interesting feature provided by the iOS SDK that allows developers to launch system apps and third-party apps through URLs. For example, let’s say your app displays a phone number, and you want to make a call whenever a user taps that number. You can use a specific URL scheme to launch the built-in phone app and dial the number automatically. Similarly, you can use another URL scheme to launch the Message app for sending an SMS.
How to make a phone call
We can launch phone calls from our iOS apps using the url scheme: tel://
.
func callTo(phoneNumber: String) -> Bool { if !phoneNumber.isEmpty { if let url = URL(string: "tel://" + phoneNumber) { if UIApplication.shared.canOpenURL(url) { UIApplication.shared.openURL(url, options: [:], completionHandler: nil) return true } } } return false }
Run your application on the Device instead of Simulator.
How to send a sms
There are some cases where you would like the user to send a text message instead of a phone call. This can include sharing content or an application.
Following is a simple example of sending texts from an iOS
import UIKit import MessageUI class TextingViewController: UIViewController, MFMessageComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() } @IBAction func clicked(_ sender: Any) { if (MFMessageComposeViewController.canSendText()) { let controller = MFMessageComposeViewController() controller.body = "Happy Birthday!" controller.recipients = ["0123456789"] controller.messageComposeDelegate = self self.present(controller, animated: true, completion: nil) } } func messageComposeViewController(_ controller: MFMessageComposeViewController!, didFinishWith result: MessageComposeResult) { switch (result) { case .cancelled: print("Message was cancelled") dismiss(animated: true, completion: nil) case .failed: print("Message failed") dismiss(animated: true, completion: nil) case .sent: print("Message was sent") dismiss(animated: true, completion: nil) default: break } } }
How to send an email
Sending email is often a common feature required for iOS apps. The most common use case for this would be for customer support emails. Having a button where the user can send an email to the developers is a key feature that enables apps to improve.
import UIKit import MessageUI class EmailViewController: UIViewController, MFMailComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() } @IBAction func clicked(_ sender: Any) { if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["test@test.test"]) mail.setSubject("Greeting") mail.setMessageBody("<p>Good morning!</p>", isHTML: true) //add attachment if let filePath = Bundle.main.path(forResource: "greeting", ofType: "json") { if let data = NSData(contentsOfFile: filePath) { mail.addAttachmentData(data as Data, mimeType: "application/json" , fileName: "greeting.json") } } present(mail, animated: true) } } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true) } }
Run your application on the Device instead of Simulator.