Saturday, June 23, 2012

How to Send an SMS Progammatically

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 protocolMFMessageComposeViewControllerDelegate. 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 ofMFMessageComposeViewController 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 methodmessageComposeViewController 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 theMessageUI.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

No comments:

Post a Comment