How to Send an SMS Progammatically

This post shows the basics for sending an SMS message from within an iPhone application. The class you’ll need to use is MFMessageComposeViewController which presents the standard SMS interface for composing and sending messages. As you’ll see in the example that follows, you can also pre-populate the body of the message as well one or more recipients for the SMS.

SMS View Controller Interface

The view controller that will send the SMS is shown below, notice the message composer import statement as well as the reference to the protocol MFMessageComposeViewControllerDelegate. The delegate has just one method where you can check the result of the message (sent, cancelled or failed) and this is also where you dismiss the view controller show the message composer – more on this method in a moment.

#import <UIKit/UIKit.h>
#import <MessageUI/MFMessageComposeViewController.h>
@interface TestViewController : UIViewController <MFMessageComposeViewControllerDelegate>
  UIButton *buttonSMS;
SMS View Controller Implementation

The implementation code begins with a very simple loadView method, this is where the view is created and a button defined to initiate sending the SMS:

- (void)loadView 
  [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];  
  buttonSMS = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [buttonSMS setFrame:CGRectMake(0, 0, 180, 40)];
  [buttonSMS setCenter:CGPointMake(160, 208)];
  [buttonSMS setTitle:@"Send SMS" forState:UIControlStateNormal];
  [buttonSMS addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
  [[self view] addSubview:buttonSMS];

Notice in the button code above the target and action values, when a touch up inside event occurs, the method buttonPressed is called. Inside this method we call a method to send an SMS, passing in the body of the SMS message as well as the phone numbers of the recipients of the message:

- (void)buttonPressed:(UIButton *)button
  if (button == buttonSMS)
    [self sendSMS:@"Body of SMS..." recipientList:[NSArray arrayWithObjects:@"+1-111-222-3333", @"111-333-4444", nil]];
Sending an SMS

The last two methods to cover manage creating an instance of MFMessageComposeViewController to create the SMS content and another method for handling the user interaction with the SMS dialog.

- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
  MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
  if([MFMessageComposeViewController canSendText])
    controller.body = bodyOfMessage;    
    controller.recipients = recipients;
    controller.messageComposeDelegate = self;
    [self presentModalViewController:controller animated:YES];
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
  [self dismissModalViewControllerAnimated:YES];
  if (result == MessageComposeResultCancelled)
    NSLog(@"Message cancelled")
  else if (result == MessageComposeResultSent)
    NSLog(@"Message sent")  
    NSLog(@"Message failed")  

In the sendSMS method, the first check is to ensure the device supports sending messages. Upon success, the body, recipients and delegate are set. The last step is to present a modal view controller to show the SMS dialog.

Once the user taps either the cancel or send on the SMS dialog, the method messageComposeViewController is called, here you can check whether the message was sent, cancelled or failed. This is also the time to dismiss the view controller presented in the previous method.

Build the Project

One last step before you can compile the application, you’ll need to add the MessageUI.framework to your project.

SMS on Simulator

You cannot send SMS messages from within the simulator, if you do, you will see the message shown below:

Send SMS Xcode Project

Download the send SMS Xcode project.


  1. Is there a way to make the ‘send’ button get pressed automatically (programmatically)?

  2. Is there any code to send sms to another source from simulator…?

  3. Is there a way to add an attachment tol the message (eg a png image)



  4. MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];

    the controller take up to a few second to load. any idea why? any way to fix this?

    thank you.

    • It will load faster second time You do it. So – i Prefer, loading/unloading at the beginning, so that – when I really need it – it will be faster.

  5. Is it necessary to pre-populate recipients for the message?

  6. Hey
    I am developing an application that sends sms to the selected contacts at regular interval for a specified number of times. Its like it should the same sms for the number of times specified by the user to the selected contacts with a time delay.
    I have the sms sending functionality working also sending it to the users multiple times by simply adding their numbers the x number of times the user has specified. But I need to add a delay between sending sms after each round.
    For example if I want a message M to be sent to S and D in my contacts, I need the app to wait for a N seconds before sending the same message again to S and D again. How can I get this working? How do I add the delay function between sending the messages to the contacts?

    Thanks in advance!

  7. Hi Kamlesh,

    if it is possible to send SMS automatically without open MFMessageComposeViewController and press send button.

    I want to send multiple SMS without open composer and tap on SEND button.

    Please help on it.

  8. Hi! I am working on a project were the user can write the text inside a text box, then send it by pushing send, very similar to the app above. Do you know how that could be written, I’m guessing its i minor change in code?

    • Sounds like you will need to grab the text from the textbox and pass the data into the sendSMS method.

  9. I am trying to complete this in xcode 4.3.1

    I am getting two errors do you know how to fix these and What version did you use?

    1)”_OBJC_CLASS_$_MFMessageComposeViewController”, referenced from:
    objc-class-ref in ViewController.o
    ld: symbol(s) not found for architecture i386

    2) clang: error: linker command failed with exit code 1 (use -v to see invocation)

    ld: warning: ignoring file /Users/jmurphy/Documents/Development/smstest/MessageUI.framework/MessageUI, file was built for unsupported file format which is not the architecture being linked (i386)
    Undefined symbols for architecture i386:
    “_OBJC_CLASS_$_MFMessageComposeViewController”, referenced from:
    objc-class-ref in ViewController.o
    ld: symbol(s) not found for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

  10. When my text button is pressed it does everything correctly, opens up the text, and displays the message to be sent. But after the message is sent It wont take me back into the app to use other functions? How do i end the Text to take me back to the program after the message is sent?

    please help thank you!

    • Make sure you have the method with the signature:

      – (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

      and in there dismiss the modal view controller.

  11. thanks for this tutorial. I just got finished launching my first iPhone app called Red Nose Cards, and this tutorial was a big help. It’s easy to foliow and well written. Thanks for putting this out there.

Comments are closed.