Understanding SMS Sending on iPhone: A Technical Deep Dive
Sending an SMS programmatically on an iPhone involves using the MFMessageComposeController
class, which is part of the MessageUI framework. In this article, we will delve into the technical aspects of sending SMSs from an iPhone app.
Introduction to MFMessageComposeController
The MFMessageComposeViewController
class is used to compose and send SMS messages programmatically. To use this class, your app must conform to the MFMessageComposeViewControllerDelegate
protocol. This delegate protocol provides methods for handling events related to message composition and sending.
Creating an Instance of MFMessageComposeController
To create an instance of MFMessageComposeController
, you use the init
method:
MFMessageComposeController *smsComposer = [[MFMessageComposeController alloc] init];
This creates a new instance of MFMessageComposeController
with default settings.
Configuring MFMessageComposeController
You can configure the MFMessageComposeController
instance by setting its properties. Here are some common properties and their uses:
recipients
: An array of phone numbers to which the message will be sent.body
: The text content of the message.delegate
: The delegate object that will receive notifications when the user interacts with the message composer.
Here is an example of how you can configure MFMessageComposeController
:
smsComposer.recipients = [NSArray arrayWithObject:@"12345678"];
smsComposer.body = @"SMS BODY HERE";
smsComposer.delegate = self;
In this example, we set the recipient to "12345678"
and the message body to "SMS BODY HERE"
. We also set the delegate property to self
, which means that our app will receive notifications when the user interacts with the message composer.
Presenting MFMessageComposeController
To present the MFMessageComposeController
instance, you use the presentModalViewController:animated:
method:
[self presentModalViewController:smsComposer animated:NO];
This presents the message composer in a modal view. The animated:
parameter is set to NO
, which means that the transition will be performed without animation.
Handling Results
When the user interacts with the message composer, our app’s delegate (self
in this case) receives notifications through the messageComposeViewController:didFinishWithResult:
method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
// Handle result here
}
The MessageComposeResult
enum provides several values that indicate what happened to the message:
kMessageComposeFailed
: The user canceled the message composition process.kMessageComposeCompleted
: The message was sent successfully.kMessageComposeRejected
: The device is unable to send SMS messages (e.g., no cellular connection).kMessageComposeDelayed
: The message is being queued and will be sent as soon as possible.
In this example, we simply dismiss the modal view controller:
[self dismissModalViewControllerAnimated:NO];
However, you may want to handle the result in a more meaningful way depending on your app’s requirements.
Opening SMS App Directly
If you don’t need to customize the message composer interface or want to open the SMS app directly without composing a message, you can use the following code:
NSString *smsURL = @"sms:12345678";
NSURL *url = [NSURL URLWithString:smsURL];
[[UIApplication sharedApplication] openURL:url];
This code creates an NSURL
object with the scheme sms
and the recipient phone number. It then presents this URL to the app’s UIApplication
instance, which will launch the SMS app.
Conclusion
Sending SMS programmatically on an iPhone involves using the MFMessageComposeController
class from the MessageUI framework. By understanding how to create and configure instances of this controller, you can implement SMS sending functionality in your iOS apps. Additionally, you can use the openURL:
method to open the SMS app directly without composing a message.
Last modified on 2023-06-20