Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a precomposed SMS message for the user in iOS SDK

I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?

like image 534
Caleb Daniels Avatar asked Dec 21 '22 03:12

Caleb Daniels


1 Answers

First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.

Now to send an SMS:

- (IBAction) sendSMS:(id)sender
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"The body of the SMS you want";
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }
    [controller release];
}

To catch the result of the sending operation:

- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch(result)
    {
        case MessageComposeResultCancelled: break; //handle cancelled event
        case MessageComposeResultFailed: break; //handle failed event
        case MessageComposeResultSent: break; //handle sent event
    }
    [self dismissModalViewControllerAnimated:YES];
}
like image 123
antf Avatar answered Dec 28 '22 11:12

antf