Sending SMS in Swift (iOS/macOS): MessageUI Library API

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending SMS in Swift (iOS/macOS): MessageUI Library API



In modern mobile development, the ability to send SMS directly from an app remains a sought-after feature. On Apple platforms (iOS and macOS), this task is handled by the built-in MessageUI.framework library. It provides a convenient interface for creating and sending text messages without requiring third-party services. In this article, we will thoroughly examine how to use the MessageUI API to send SMS in Swift, look at code examples, error handling, and specifics of working on different devices.



What is MessageUI.framework?

MessageUI is an Apple system framework that allows applications to integrate with the built-in Messages app. The main class for working with SMS is MFMessageComposeViewController. It provides a ready-made UI controller for composing and sending messages. It is important to note that this API only works on real devices with an active SIM card (for iOS) or on a Mac with iMessage configured (macOS 10.15+). The functionality is not available on the simulator.



Project Setup

To use MessageUI, you need to:

  • Add the MessageUI framework to the project (it is usually already in the standard library).
  • Add an import in the file where the sending will be called: import MessageUI.
  • Ensure the device can send SMS — check via MFMessageComposeViewController.canSendText().


Basic SMS Sending Example

Let's look at a simple example: creating a controller, setting the recipient and text, then displaying it.



import UIKitimport MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

@IBAction func sendSMSTapped(_ sender: UIButton) { // Check if the device can send SMS guard MFMessageComposeViewController.canSendText() else { print("Device does not support sending SMS") return }

// Create an instance of the controller let messageVC = MFMessageComposeViewController() messageVC.messageComposeDelegate = self

// Set the recipient messageVC.recipients = ["+79991234567"]

// Set the message text messageVC.body = "Hello! This is a test message from my app."

// Display the controller present(messageVC, animated: true, completion: nil) }

// MARK: - MFMessageComposeViewControllerDelegate func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { // Dismiss the controller after sending/cancelling dismiss(animated: true, completion: nil)

// Handle the result switch result { case .sent: print("Message sent successfully") case .cancelled: print("Sending cancelled by user") case .failed: print("Error sending message") @unknown default: print("Unknown result") } }}


In this code, we:

  • Check the ability to send using canSendText().
  • Create an MFMessageComposeViewController and configure recipients and the message body.
  • Set a delegate (messageComposeDelegate) to handle the result.
  • In the delegate method, handle four possible outcomes: sent, cancelled, error, unknown status.


Additional Features

MessageUI supports not only plain text but also attachments:



// Adding an attachment (e.g., an image)if let imageData = UIImage(named: "photo")?.pngData() {    messageVC.addAttachmentD                    

Recommendations