How to Send an SMS Progammatically

June 1, 2011

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;
}
 
@end
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")  
  else 
    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.

25 comments

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

by Boot on Aug 8, 2011. Reply #

Unfortunately, there is not, the user must acknowledge the message.

by John Muchow on Aug 11, 2011. Reply #

thax………..

by Raj on Aug 10, 2011. Reply #

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

by remo on Dec 25, 2011. Reply #

The simulator does not support sms

by John Muchow on Dec 26, 2011. Reply #

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

cheers

Paul

by Paul Mitchell on Jan 10, 2012. Reply #

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.

by Jimmy on Jan 29, 2012. Reply #

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.

by GT on Oct 15, 2012. Reply #

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

by Aldee on Jan 31, 2012. Reply #

No, you do not need to add any value for recipients.

by John Muchow on Feb 5, 2012. Reply #

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!

by Kamalesh on Feb 24, 2012. Reply #

I would recommend looking into the NSTimer object.

by John Muchow on Feb 24, 2012. Reply #

Hey John,
thanks for replying. I am not quite clear how to get it working. Can you give me a better insight on how to use the NSTimer object to achieve this?
Thanks!

by kamalesh on Feb 24, 2012. Reply #

There is one example here: http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html I would also look at the documentation for the API.

by John Muchow on Feb 24, 2012. Reply #

Very good tutorial….
TQ
Narayana

by Narayana on Jun 5, 2012. Reply #

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.

by Hardik Darji on Jul 6, 2012. Reply #

There is no way to send messages without user interaction.

by John Muchow on Jul 9, 2012. Reply #

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?

by Maath Mahdi on Aug 18, 2012. Reply #

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

by John Muchow on Aug 18, 2012. Reply #

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)

by JJmurph on Sep 19, 2012. Reply #

Make sure the MessageUI framework is in the project and there is also an import for the API

by John Muchow on Sep 19, 2012. Reply #

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!

by jonathan on Dec 15, 2012. Reply #

Make sure you have the method with the signature:

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

and in there dismiss the modal view controller.

by John Muchow on Dec 16, 2012. Reply #

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.

by Mark Lopez on Dec 22, 2012. Reply #

Thanks for this topic, I found it useful even for iOS 7. But can you please update it in order to change methods deprecated in iOS 6 to new ones please?

by Александр Чередниченко on Mar 17, 2014. Reply #

Leave a Comment