Send SMS From The iPhone OS4

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 <uikit/UIKit.h>
#import <messageUI/MessageUI.h>
#import <messageUI/MFMessageComposeViewController.h>

@interface SMSSendViewController : UIViewController {

UILabel *SMS;
}

@property (nonatomic, retain) UILabel *SMS;

-(void)ComposerSheet;

 

 

Step 6: Open the SMSSendViewController.m file and make the following changes:

 

- (void)viewDidLoad {
[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

Leave a Comment:

7 comments
kewl says July 2, 2010

does this work for ipad 3.2?

Reply
Sushant says July 3, 2010

No,This application works only for iPhone OS4.

Reply
einstein_katro says July 7, 2010

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.

Reply
sacristan says July 30, 2010

Could send messages withoyut launch the SMS app of the i-OS, because i want to send a message automatically without the user intervention (not malefic end xD).
Thanks in advance

Reply
Sacristan says July 30, 2010

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

Reply
faraz says October 12, 2010

you use hardcoaded number is there any way we can use numbers from our contact list?

Reply
John says March 21, 2011

Hi,

Good post, exactly what I needed. Thanks. Hopefully Apple will enable this SMS messaging for iPad 3G.

John.

Reply
Add Your Reply