Introduction (真机)
I will demo some sample to present how to send SMS in iphone programming.
Sample 1
The easiest way is this demo.
- [[UIApplication sharedApplication] openURL: @"sms:12345678"];
- HTML links:
- <a href="sms:" mce_href="sms:">Launch Text Application</a>
- <a href="sms:1-408-555-1212" mce_href="sms:1-408-555-1212">New SMS Message</a>
- Native application URL strings:
- sms:
- sms:1-408-555-1212
Sample 2
We can use MessageUI Framework and MFMessageComposeViewController to send SMS.
- -(IBAction) sendInAppSMS:(id) sender
- {
- MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
- if([MFMessageComposeViewController canSendText])
- {
- controller.body = @"Hello from Mugunth";
- controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
- controller.messageComposeDelegate = self;
- [self presentModalViewController:controller animated:YES];
- }
- }
You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
- {
- switch (result) {
- case MessageComposeResultCancelled:
- NSLog(@"Cancelled");
- break;
- case MessageComposeResultFailed:
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
- delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
- [alert show];
- [alert release];
- break;
- case MessageComposeResultSent:
- break;
- default:
- break;
- }
- [self dismissModalViewControllerAnimated:YES];
- }
Sample 3
This is a full sample code for MFMessageComposeViewController.
- // 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
- // 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];
- icker.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