iOS tutorial: Send SMS programmatically

iOS is very strict when it comes to using phone capabilities in applications. It limits usage because of security reasons, so in fact, as an iPhone application developer you can’t send SMS in background or listen for incoming SMS messages. The only solution to send SMS is to initiate the built in SMS application where user can click to send message (however application can preset the SMS receiver and body). This is what I show in this tutorial:

1. Import MessageUI

Add import statement in header file:

#import <MessageUI/MessageUI.h>

2. Use delegate in header

in your header file, declare the delegate as:

<MFMessageComposeViewControllerDelegate>

3. Initiate the send SMS request

Depending on your application logic, you can use this method in various places. In my case this is initiated on button click. ‘body‘ param is message content.

- (void) sendSMS:(NSString*)body{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewControlleralloc] init];
    if([MFMessageComposeViewControllercanSendText])
    {
        NSString *num = [[NSUserDefaultsstandardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];

        controller.body = body;
        controller.recipients = [NSArray arrayWithObjects:@"1(234)567-8910", num, nil];
        controller.messageComposeDelegate = self;
        [selfpresentViewController:controller animated:YEScompletion:nil];
    }
}

4. React on user’s confirmation

Code from sendSMS method will open SMS application with prepared SMS message and confirmation and cancellation button. To react on these button clicks, you have to implement delegate method:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    NSString *num = [[NSUserDefaultsstandardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];
    NSLog(@"Some result received: %u, %@", result, num);
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}

Is that really not possible?

More advanced phone actions are possible on Jailbroken devices and by using undocumented APIs, but Apple will not approve such application on AppStore.

And on Android?

In Android the situation is different. Developer is free to use all phone features including sending SMS in background, listening for incoming SMS, calls, initiating calls etc. It trusts user that if he install app he agrees that it may use such features on his device.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

3 thoughts on “iOS tutorial: Send SMS programmatically

  1. It’s really a great and helpful piece of info.
    I am glad that you simply shared this useful info with us.
    Please stay us informed like this. Thanks for sharing.

    1. I am new in ios, and i want to send address through sms, i already get the address of users current location and want to send that address label through sms on button click, please provide whole source code to me for that.

      1. Just check the basic tutorials on handling button clicks in iOS :) There is plenty of them and they are good. This is not the subject of that post

Give Your feedback: