Sending Text messages from an App

With the MessageUI FrameWork we can easily add email and messaging functionality in our app. In this tutorial we will send a message using the MFMessageComposeViewController object.

Update: Nov 25, 2014. The rewritten version in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use MessageDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

First, Let's create our User Interface. Go to ViewController.xib. Drag a button to the main View and give it a name of "Text me". The main View should look like this.

In ViewController.m in the interface section declare the following IBAction method

@interface ViewController()

- (IBACtion)sendMessage:(id)sender;


Go back to ViewController.xib. Select the Assistant Editor. On the right side select ViewController.m. Ctrl-click and drag from to Button to the IBAction method in ViewController.m.

Navigate to the Project Settings at the Linked Frameworks and Libraries section. Add the Message Framework.

In ViewController.m, import the Message Framework.

#import <MessageUI/MessageUI.h>

Our ViewController must be able to respond to updates from a message compositon, so it must the conform to the MFMessageComposeViewControllerDelegate protocol. Change the interface line to

@interface ViewController() <MFMEssageComposeViewControllerDelegate>

Next, we implement the sendMessage IBAction method

- (IBAction)sendMessage:(id)sender
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];

messageVC.body = @"Enter a message";
messageVC.recipients = @[@"Telnumber"];
messageVC.messageComposeDelegate = self;

[self presentViewController:messageVC animated:NO completion:NULL];

We initialize a MFMessageComposeViewController and fill in the body and receptiens. Fill in your own values. The the View Controller is presentented. The user can edit the body and change the recipients if they like, and press the send button.

To conform the view controller to the MFMessageComposeViewControllerDelegate protocol, the messageComposeViewController:didFinishWithResult method is required. Here we check if the messaging has succeeded or not and we dismiss our view controller.

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"Message was cancelled");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultFailed:
NSLog(@"Message failed");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultSent:
NSLog(@"Message was sent");
[self dismissViewControllerAnimated:YES completion:NULL]; break;


Messaging doesn't work in the iOS Simulator, so it should run on a device. Build and Run and start messaging.

You can download the source code of the MessageDemo at the ioscreator repository on github.