Sunday, April 18, 2010

MFMessageComposeViewController Sample Code (OS 4.0 only)

Start a new View-based Application Project called SMS2
and have to add the MessageUI framework to the project

Modify SMS2ViewController.h

  SMS2ViewController.h    Select all

// SMS2ViewController.h
// SMS2

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface SMS2ViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
UILabel *message;
}

@property (nonatomic, retain) UILabel *message;

-(void)displayComposerSheet;

@end


and in SMS2ViewController.m

  SMS2ViewController.m    Select all

// SMS2ViewController.m
// SMS2

#import "SMS2ViewController.h"

@implementation SMS2ViewController
@synthesize message;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
/*
- (void)loadView {

}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (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:@" Send SMS" 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(displayComposerSheet) forControlEvents:UIControlEventTouchUpInside];

message = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 360.0, 280.0, 29.0)];
message.frame = CGRectMake(20.0, 360.0, 280.0, 29.0);
message.adjustsFontSizeToFitWidth = YES;
message.hidden = YES;
message.text = @"";
message.userInteractionEnabled = NO;

[self.view addSubview:smsButton];
[self.view addSubview:message];
}


-(void)displayComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

picker.recipients = [NSArray arrayWithObject:@"123456789"]; // your recipient number or self for testing
picker.body = @"test from OS4";

[self presentModalViewController:picker animated:YES];
[picker release];
NSLog(@"SMS fired");
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
message.hidden = NO;
switch (result)
{
case MessageComposeResultCancelled:
message.text = @"Result: canceled";
NSLog(@"Result: canceled");
break;
case MessageComposeResultSent:
message.text = @"Result: sent";
NSLog(@"Result: sent");
break;
case MessageComposeResultFailed:
message.text = @"Result: failed";
NSLog(@"Result: failed");
break;
default:
message.text = @"Result: not sent";
NSLog(@"Result: not sent");
break;
}

[self dismissModalViewControllerAnimated:YES];

}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}

@end




.
.

10 comments:

MrP said...

I've tried your code on my fresh installed XCode 3.2.3, but the modal view presented in displayComposerSheet is empty.
The console output is

Application tried to push a nil view controller on target

Did someone have the same issue?

javacom said...

You can only use this code on iPhone devices, not on iPod Touch nor iPhone Simulator to send SMS

Keith said...

Simulator can't send SMS is logical. But even can't show the SMS composer view?

Unknown said...

I'm lookng to see if there is access to the SMS log via this API. So, if i use this method could I also rcv SMS messages or will it drive me to the iphone SMS. Is it sending via the user account SMS(like in the USA, ATT) or do you have to drive to a third party gateway.

Sorry lots of questions

Unknown said...

XCode 3.2.3 won't work. You need to use XCode 4.0

Rene Pardon said...

Thank you for your awesome post.

Is it also possible to send a sms without opening the SMS-App? (in iOS4)
I like to ask the user, if he really will send a SMS or not. But the message text shouldn't be displayed for security reaons.


regards
Ren´e

Anonymous said...

2010-07-02 09:08:14.219 SMS2[1892:307] -[SMS2ViewController length]: unrecognized selector sent to instance 0x1402d0

schwabsauce said...

actually XCode 4 seems to have the same issue, just about. works on the device though. oddly XCode seems to return YES from the +canSendText method.

Anonymous said...

Can we Auto Send SMS at a particular date and time

Anonymous said...

Thanks. I followed the docs and somehow my recip prop didnt work