Sending SMS in Swift (iOS/macOS): API and Libraries
In iOS and macOS development, there is often a need to send SMS messages — for user verification, sending notifications, or integrating with CRM. In this article, we will look at two main approaches: using the built-in MessageUI framework to send SMS through the system application, and integration with cloud SMS providers (Twilio, Nexmo/Vonage) via HTTP API. We will also discuss limitations and best practices.
1. Sending SMS via MessageUI (iOS only)
The MessageUI framework provides the MFMessageComposeViewController controller, which allows you to send an SMS from your app using the standard iOS interface. Important: this method only works on real devices (not in the simulator) and requires an active SIM card.
Step 1: Import the framework
Add the import to the file where you will work with SMS:
import MessageUIStep 2: Check if sending is possible
Before calling the controller, always check if the device can send SMS:
if MFMessageComposeViewController.canSendText() { // can send} else { // show error to user}Step 3: Create and configure the controller
let messageVC = MFMessageComposeViewController()messageVC.body = "Your confirmation code: 123456"messageVC.recipients = ["+79001234567"]messageVC.messageComposeDelegate = self // don't forget to set the delegatepresent(messageVC, animated: true, completion: nil)Step 4: Implement the delegate
extension YourViewController: MFMessageComposeViewControllerDelegate { func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { controller.dismiss(animated: true) { switch result { case .sent: print("SMS sent") case .cancelled: print("Cancelled") case .failed: print("Send failed") @unknown default: break } } }}Limitations:
- Does not work in the simulator.
- The user sees the interface and can modify the message.
- Not suitable for automatic sending (e.g., server-side).
- Not available on macOS (iOS only).
2. Sending SMS via Third-Party APIs (Twilio, Nexmo, etc.)
For server-side SMS sending (without user interaction), use the HTTP API of cloud providers. Let's look at an example with Twilio — one of the most popular services.
Step 1: Obtain credentials
Register with Twilio, get your Account SID, Auth Token, and a sender phone number (e.g., +15005550006 for testing).
Step 2: Send a POST request
Use URLSession to call the Twilio API:
func sendSMSviaTwilio(to recipient: String, message: String) { let accountSID = "YOUR_ACCOUNT_SID" let authToken = "YOUR_AUTH_TOKEN" let fromNumber = "+15005550006" 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)" let loginData = loginString.data(using: .utf8)! let base64LoginString = loginData.base64EncodedString() request.setValue("Basic \\(base64LoginString)", forHTTPHeaderField: "Authorization") let bodyParams = [ "From": fromNumber, "To": recipient, "Body": message ] le Also in library
Best Logging Libraries for Go (Golang): Overview and Comparison
PDF Generation in C# (.NET): Overview of Libraries and Tools