Sending SMS in Swift (iOS/macOS): overview of libraries and APIs
In modern mobile development, the ability to send SMS messages remains a sought-after feature: from phone number verification to mass notifications. In the Apple ecosystem (iOS/macOS) using Swift, there are several approaches — from the built-in MessageUI to integration with cloud SMS providers. In this article, we will review the main libraries and APIs, their advantages and limitations, and provide practical code examples.
1. Built-in MessageUI library
The simplest way is to use the MessageUI framework, available on iOS and macOS. It allows you to invoke the standard SMS sending interface (via MFMessageComposeViewController) without connecting third-party SDKs.
Capabilities:
- Sending SMS through the system Messages app.
- Support for attachments (images, videos, contacts).
- No provider registration required.
Limitations:
- Works only on physical devices (not in the simulator).
- The user sees the interface and confirms sending.
- Not suitable for automatic (server-side) sending.
Code example in Swift (iOS):
import MessageUIimport UIKit
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
@IBAction func sendSMSTapped(_ sender: UIButton) { guard MFMessageComposeViewController.canSendText() else { print("Device does not support sending SMS") return } let messageVC = MFMessageComposeViewController() messageVC.body = "Hello! This is a test SMS from Swift." messageVC.recipients = ["+79123456789"] messageVC.messageComposeDelegate = self present(messageVC, animated: true, completion: nil) }
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { switch result { case .sent: print("SMS sent") case .cancelled: print("Cancelled by user") case .failed: print("Sending failed") @unknown default: break } controller.dismiss(animated: true, completion: nil) }}For macOS, NSSharingService with type .composeMessage is used, but its functionality is limited.
2. Twilio — cloud API for SMS
Twilio is one of the most popular services for programmatic SMS sending. Integration into a Swift application is done via the REST API or the official Swift client twilio-swift.
Advantages:
- High delivery speed, support for over 100 countries.
- Ability to send without user involvement (server-side logic).
- Flexible settings: sender, templates, webhooks.
Example of sending via URLSession:
import Foundation
func sendSMSviaTwilio(to phoneNumber: String, message: String) { let accountSID = "YOUR_ACCOUNT_SID" let authToken = "YOUR_AUTH_TOKEN" let fromNumber = "+1234567890" // your Twilio number let urlString = "https://api.twilio.com/2010-04-01/Accounts/\\(accountSID)/Messages.json" guard let url = URL(string: urlString) else { return } var request = URLRequest(url: url) request.httpMethod = "POST" let loginString = "\\(accountSID):\\(authToken)" guard let loginData = loginString.data(using: .utf8) else { return } let base64Login = loginData.base64EncodedString() request.setValue("Basic \\(base64Login)", forHTTPHeaderField: "Authorization") let bodyParams = "To=\\(phoneNumber)&From=\\(fromNumber)&Body=\\(message)"