Tuesday, May 29, 2012

How to programmatically send SMS on the iPhone?


Does anybody know if it's possible, and how, to programmatically send a SMS from the iPhone, with the official SDK / Cocoa Touch?



Source: Tips4all

10 comments:

  1. Restrictions

    If you could send an SMS within a program on the iPhone, you'll be able to write games that spam people in the background. I'm sure you really want to have spams from your friends, "Try out this new game! It roxxers my boxxers, and yours will be too! roxxersboxxers.com!!!! If you sign up now you'll get 3,200 RB points!!"

    Apple has restrictions for automated (or even partially automated) SMS and dialing operations. (Imagine if the game instead dialed 911 at a particular time of day)

    Your best bet is to setup an intermediate server on the internet that uses an online SMS sending service, and send the SMSs via that route if you need complete automation. (ie, your program on the iPhone sends a UDP packet to your server, which sends the real SMS)

    iOS 4 Update

    iOS 4, however, now provides a viewcontroller you can import into your application. You prepopulate the SMS fields, then the user can initiate the SMS send within the controller. Unlike using the "sms:..." url format, this allows your application to stay open, and allows you to populate both the to and the body fields. You can even specify multiple recipients.

    This prevents applications from sending automated SMS without the user explicitly aware of it. You still cannot send fully automated SMS from the iPhone itself, it requires some user interaction. But this at least allows you to populate everything, and avoids closing the application.

    The MFMessageComposeViewController class is well documented, and tutorials show how easy it is to implement.

    iOS 5 Update

    iOS 5 includes messaging for iPod touch and iPad devices, so while I've not yet tested this myself, it may be that all iOS devices will be able to send SMS via MFMessageComposeViewController. If this is the case, then Apple is running an SMS server that sends messages on behalf of devices that don't have a cellular modem.

    Limitations to this class

    Keep in mind that this won't work on phones without iOS 4, and it won't work on the iPod touch or the iPad, except, perhaps, under iOS 5. You must either detect the device and iOS limitations prior to using this controller, or risk restricting your app to recently upgraded 3G, 3GS, and 4 iPhones.

    However, an intermediate server that sends SMS will allow any and all of these iOS devices to send SMS as long as they have internet access, so it may still be a better solution for many applications. Alternately, use both, and only fall back to an online SMS service when the device doesn't support it.

    ReplyDelete
  2. You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text (see this post on Apple Developer Forums).

    ReplyDelete
  3. In App SMS is finally supported in iPhone OS 4.

    ReplyDelete
  4. Here is a tutorial which does exactly what you are looking for: the MFMessageComposeViewController.

    http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/

    And a link to the docs.

    http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

    ReplyDelete
  5. For anyone searching for the SMS API, here is the documentation. Googling a little about the subject, I've found some forums (here and here) talking about CTMessageCenter (from CoreTelephony Framework) in OS 2 and 3, but not working in the new OS. I'm going to test both approaches and comment in this post.

    Edit: It's important to note that using API's not released to developers by Apple may incur in your app being blocked, so use this link at your own risk.

    Note: I've posted here since I'm note allowed to comment yet.

    ReplyDelete
  6. you must add MessageUI.framework to your Xcode project
    include a #import <MessageUI/MessageUI.h> in your header file
    Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate
    In your IBAction method declare instance of MFMessageComposeViewController say messageInstance
    to check whether your device can send text use [MFMessageComposeViewController canSendText] in if condition, it'll return Yes/No
    in the if condition do these:


    first set body for your messageInstance as:

    messageInstance.body = @"Hello from Shah";

    then decide the recipients for the message as:

    messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];

    set a delegate to your messageInstance as:

    messageInstance.messageComposeDelegate = self;

    In last do this

    [self presentModalViewController:controller animated:YES];

    ReplyDelete
  7. With ios in-app sms is supported. Here is the class def for that

    http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

    ReplyDelete
  8. There is a class in iOS 4 which supports sending messages with body and recipents from your application. It works the same as sending mail. You can find the documentation here: link text

    ReplyDelete
  9. Use this:

    - (void)showSMSPicker
    {
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil) {
    // Check whether the current device is configured for sending SMS messages
    if ([messageClass canSendText]) {
    [self displaySMSComposerSheet];
    }
    }
    }

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
    {
    //feedbackMsg.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
    {
    case MessageComposeResultCancelled:
    {
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert1 show];
    [alert1 release];
    }

    // feedbackMsg.text = @"Result: SMS sending canceled";
    break;

    case MessageComposeResultSent:
    {
    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert2 show];
    [alert2 release];
    }

    // feedbackMsg.text = @"Result: SMS sent";
    break;

    case MessageComposeResultFailed:
    {
    UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert3 show];
    [alert3 release];
    }

    // feedbackMsg.text = @"Result: SMS sending failed";
    break;

    default:
    {
    UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert4 show];
    [alert4 release];
    }

    // feedbackMsg.text = @"Result: SMS not sent";
    break;
    }

    [self dismissModalViewControllerAnimated: YES];
    }

    ReplyDelete
  10. If you want, you can use the private framework CoreTelephony which called "CTMessageCenter" class. There are a few methods to send sms.

    ReplyDelete