MFMessageComposeViewController Sample Code (OS 4.0 only)


Via: http://iphonesdkdev.blogspot.com/2010/04/mfmessagecomposeviewcontroller-sample.html

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

Modify SMS2ViewController.h


// SMS2ViewController.h
// SMS2

#import
#import
#import

@interface SMS2ViewController : UIViewController {
UILabel *message;
}

@property (nonatomic, retain) UILabel *message;

-(void)displayComposerSheet;

@end



and in SMS2ViewController.m


// 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

Comments