Officially, iPhone OS 4 is out of NDA and I can’t write a post on this. If you have been reading my blogs, you might already know how to send a in-app email Sending a in-app SMS is very similar to this, but with subtle differences.

Prior to iPhone OS 4, developers have to depend on

[[UIApplication sharedApplication] openURL: @"sms:12345678"];

The problem with this is not just that it closes your app, but there is no way to specify the body content of the SMS. Secondly, you are restricted to send the SMS to only one person. However, with the new MessageUI SMS controller, you can send SMS to multiple people at the same time. You can also pre-populate the SMS body field.
Developers of famous apps like Whatsapp Messenger, copy the SMS text content to clipboard and open the SMS app to allow users to paste the content. But with this newly allowed In-App SMS sheet, users can send SMS without quitting the app.

So, Let’s get started.

Step 1:

Import the MessageUI Framework into your project and #import the header file into the “.h” file of your controller where you want to open the In-App SMS sheet.

Step 2:

You might already have a button handler IBAction where you want to send the SMS. If not create a Button on your XIB file and write IBActions for it.

Step 3:

The real code

-(IBAction) sendInAppSMS:(id) sender
	MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
	if([MFMessageComposeViewController canSendText])
		controller.body = @"Hello from Mugunth";
		controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
		controller.messageComposeDelegate = self;
		[self presentModalViewController:controller animated:YES];

The most important part here is the line [MFMessageComposeViewController canSendText].
When sending a in-app email, you can choose to ignore this (atleast as of now) because most of the devices would have upgraded to iPhone OS 3 and all those devices would have the ability to send in-app email. However, the same doesn’t apply to SMS. Remember that even if a device is running iPhone OS 4, if it’s an iPod touch, it will never be abel to send SMS within app.
In this case, I have used a if condition to send the SMS. Practically speaking, you should enable/disable the button the user taps to send the sms based on this. You can add the code that does this in your viewDidLoad method.

Secondly, you have to set the messageComposeDelegate to self and not delegate. If you set the controller.delegate to self, you will not get the didFinishWithResult callback and the In-App SMS sheet will not close.

Step 4:

Implement Delegate Callbacks.
In your header file, implement the callbacks, MFMessageComposeViewControllerDelegate and UINavigationControllerDelegate. If you don’t you will get a warning at the line,

 controller.delegate = self;

You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
	switch (result) {
		case MessageComposeResultCancelled:
		case MessageComposeResultFailed:
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
														   delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
			[alert show];
			[alert release];
		case MessageComposeResultSent:
	[self dismissModalViewControllerAnimated:YES];

That’s it. Your app should now be able to send SMS using the new Message UI sheet.

Where the heck is MMS in this tutorial?

As on date, the MFMessageComposeViewController doesn’t support sending MMS. The controller.body is a NSString and setting a NSData pointer obviously crashes the app. Hopefully, one day, Apple will allow sending In-App MMS and I’ll probably blog about that too…

Follow me on Twitter