iPhone
SMS
programming
iOS development
mobile app development

How to programmatically send SMS on the iPhone?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Sending SMS programmatically on an iPhone requires a good understanding of the iOS ecosystem, specifically leveraging the available APIs provided by Apple. As of the latest updates, iOS does not allow third-party apps to send SMS directly due to privacy and security constraints. However, it is possible to send an SMS by presenting a pre-filled message in the native Messages app using the MessageUI framework. Let's dive into the technical aspects and examples of how to achieve this.

Understanding the MessageUI Framework

The MessageUI framework provides view controllers that can manage standard email and SMS interactions within your app. For sending SMS, you can use the MFMessageComposeViewController class, which presents a view to draft and send a message. The user will have to manually tap the send button, as automatic sending is not possible.

Key Components

  • MFMessageComposeViewController: A standard system interface allowing users to compose SMS/MMS messages. Importantly, this class handles the interface while ensuring compliance with Apple's privacy guidelines.
  • MFMessageComposeViewControllerDelegate: A protocol to handle the results after the user interacts with the message interface.

Implementation Steps

  1. Import the MessageUI Framework: Ensure your project integrates the MessageUI framework.
swift
    import MessageUI
  1. Check for SMS capability: Confirm that the user’s device can send SMS.
swift
1    if MFMessageComposeViewController.canSendText() {
2        // Device can send SMS
3    } else {
4        // Handle devices that cannot send SMS
5    }
  1. Set up the MFMessageComposeViewController: Create and configure the message composer.
swift
1    func sendSMS(recipients: [String], body: String, delegate: MFMessageComposeViewControllerDelegate) {
2        if MFMessageComposeViewController.canSendText() {
3            let messageComposeVC = MFMessageComposeViewController()
4            messageComposeVC.messageComposeDelegate = delegate
5            messageComposeVC.recipients = recipients // Optional
6            messageComposeVC.body = body
7
8            // Present the message compose view controller
9            present(messageComposeVC, animated: true, completion: null)
10        } else {
11            print("SMS services are not available")
12        }
13    }
  1. Handle the message result: Implement the delegate method to handle the result of the message composition.
swift
1    extension YourViewController: MFMessageComposeViewControllerDelegate {
2        func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
3            controller.dismiss(animated: true, completion: nil)
4            switch result {
5            case .cancelled:
6                print("Message was cancelled")
7            case .failed:
8                print("Message failed")
9            case .sent:
10                print("Message was sent")
11            @unknown default:
12                break
13            }
14        }
15    }

Considerations

  • User Interaction: Because automatic sending is not possible, user interaction is required to finalize and send the message.
  • Privacy: Always inform users when their data might be shared externally. iOS protects users' privacy by requiring user interaction for sending SMS.
  • Testing: When testing, make sure the iOS device is capable of sending SMS to avoid errors. The Simulator does not support sending SMS.

Summary Table

The table below summarizes the key points related to implementing SMS functionality on an iPhone:

FeatureDescription
Capability CheckVerify if the device can send SMS
Use of MFMessageComposeViewControllerProvides the UI to compose and send messages
User Interaction RequiredUsers must manually send the SMS from the composer view
Delegate HandlingManage the result of the sending action using the MFMessageComposeViewControllerDelegate
Simulator LimitationSMS functionality cannot be tested on the iOS Simulator

Additional Details and Best Practices

  • Error Handling: Ensure robust error handling to manage cases where devices can't send SMS or network issues arise.
  • Testing on Real Devices: Given the limitations of the iOS Simulator, always test SMS functionality on a real device to verify behavior correctly.
  • Managing Permissions: While not required specifically for SMS, ensure your app has appropriate permission management and privacy statements in place.

This guide provides a foundational understanding of how to integrate SMS functionality within your iOS application, adhering to Apple's privacy and guidelines. As iOS evolves, ensure you stay updated with the latest practices as provided by Apple’s documentation.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.