This is the very simple example. In this example we will see how to SMS Send from the iPhone OS4.
Step 1: Create a View base application using template. Give the application name “SMSSend”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Xpand classes and notice Interface Builder created the SMSSendViewController class for you. Expand Resources and notice the template generated a separate nib,SMSSendViewController.xib, for the “SMSSend”.
Step 4: We need to add MessageUI.framework in the Frameworks folder. Select-> Frameworks folder -> Add ->Existing Frameworks -> then select MessageUI.framework.
Step 5: In the SMSSendViewController.h file , we have created instance of UILable class and define one method. So make the following changes in the file:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>
@interface SMSSendViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
UILabel *SMS;
}
@property (nonatomic, retain) UILabel *SMS;
-(void)ComposerSheet;
Step 6: Open the SMSSendViewController.m file and make the following changes:
[super viewDidLoad];
UIButton *smsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
smsButton.frame = CGRectMake(97.0, 301.0, 125.0, 37.0);
smsButton.adjustsImageWhenDisabled = YES;
[smsButton setTitle:@"SMS Send" forState:UIControlStateNormal];
[smsButton setTitleColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];
[smsButton setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];
[smsButton addTarget:self action:@selector(ComposerSheet) forControlEvents:UIControlEventTouchUpInside];
SMS = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 360.0, 280.0, 29.0)];
SMS.frame = CGRectMake(20.0, 360.0, 280.0, 29.0);
SMS.adjustsFontSizeToFitWidth = YES;
SMS.hidden = YES;
SMS.text = @"";
SMS.userInteractionEnabled = NO;
[self.view addSubview:smsButton];
[self.view addSubview:SMS];
}
-(void)ComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObject:@"23234567"];
picker.body = @"iPhone OS4";
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
SMS.hidden = NO;
switch (result)
{
case MessageComposeResultCancelled:
SMS.text = @"Result: canceled";
NSLog(@"Result: canceled");
break;
case MessageComposeResultSent:
SMS.text = @"Result: sent";
NSLog(@"Result: sent");
break;
case MessageComposeResultFailed:
SMS.text = @"Result: failed";
NSLog(@"Result: failed");
break;
default:
SMS.text = @"Result: not sent";
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Step 7: Now compile and run the application. Remember you need to run this application on the iPhone device not a simulator.
You can Download SourceCode from here SMSSend











does this work for ipad 3.2?
No,This application works only for iPhone OS4.
Is it also possible to send a sms without opening the SMS-App? (in iOS4)
If it is impossible, how to hide message text in MFMessageComposeViewController ?
I want to hide message text for security reasons.
with the MFMessageComposeViewController could i sent messages without the user action, without launching the SMS-App of the i-OS. (Not a malefic end xD).
Thanks in advance for comments
Hi,
Good post, exactly what I needed. Thanks. Hopefully Apple will enable this SMS messaging for iPad 3G.
John.