iPhone send message implement

Introduction (真机)

 

I will demo some sample to present how to send SMS in iphone programming.

 

Sample 1

 

The easiest way is this demo.

 

[cpp]  view plain copy
  1. [[UIApplication sharedApplication] openURL: @"sms:12345678"];  

 

[xhtml]  view plain copy
  1. HTML links:  
  2.   
  3. <a href="sms:" mce_href="sms:">Launch Text Application</a>   
  4. <a href="sms:1-408-555-1212" mce_href="sms:1-408-555-1212">New SMS Message</a>   
  5.   
  6. Native application URL strings:  
  7.   
  8. sms:   
  9. sms:1-408-555-1212   

 

Sample 2

 

We can use MessageUI Framework and MFMessageComposeViewController to send SMS.

 

[cpp]  view plain copy
  1. -(IBAction) sendInAppSMS:(id) sender  
  2. {  
  3.     MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];  
  4.     if([MFMessageComposeViewController canSendText])  
  5.     {  
  6.         controller.body = @"Hello from Mugunth";  
  7.         controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];  
  8.         controller.messageComposeDelegate = self;  
  9.         [self presentModalViewController:controller animated:YES];  
  10.     }  
  11. }  

 

You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.

 

[cpp]  view plain copy
  1. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
  2. {  
  3.     switch (result) {  
  4.         case MessageComposeResultCancelled:  
  5.             NSLog(@"Cancelled");  
  6.             break;  
  7.         case MessageComposeResultFailed:  
  8.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"  
  9.                                                            delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];  
  10.             [alert show];  
  11.             [alert release];  
  12.             break;  
  13.         case MessageComposeResultSent:  
  14.    
  15.             break;  
  16.         default:  
  17.             break;  
  18.     }  
  19.    
  20.     [self dismissModalViewControllerAnimated:YES];  
  21. }  

 

Sample 3

 

This is a full sample code for MFMessageComposeViewController.

 

[cpp]  view plain copy
  1. //  SMS2ViewController.h  
  2. //  SMS2  
  3. #import <UIKit/UIKit.h>  
  4. #import <MessageUI/MessageUI.h>  
  5. #import <MessageUI/MFMessageComposeViewController.h>  
  6. @interface SMS2ViewController : UIViewController <MFMessageComposeViewControllerDelegate>  
  7. {  
  8.     UILabel *message;  
  9. }  
  10. @property (nonatomic, retain) UILabel *message;  
  11. -(void)displayComposerSheet;  
  12. @end  

 

[cpp]  view plain copy
  1. //  SMS2ViewController.m  
  2. //  SMS2  
  3. #import "SMS2ViewController.h"  
  4. @implementation SMS2ViewController  
  5. @synthesize message;  
  6. /* 
  7. // The designated initializer. 
  8.  Override to perform setup that is required before the view is loaded. 
  9.  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
  10.  { 
  11.     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
  12.     { 
  13.         // Custom initialization 
  14.     } 
  15.     return self; 
  16. } 
  17. */  
  18. // Implement loadView to create a view hierarchy programmatically, without using a nib.  
  19. /* 
  20. - (void)loadView 
  21. { 
  22. } 
  23. */  
  24. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  25. - (void)viewDidLoad  
  26. {  
  27.     [super viewDidLoad];  
  28.     UIButton *smsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  29.     smsButton.frame = CGRectMake(97.0, 301.0, 125.0, 37.0);  
  30.     smsButton.adjustsImageWhenDisabled = YES;  
  31.     [smsButton setTitle:@" Send SMS" forState:UIControlStateNormal];  
  32.     [smsButton setTitleColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];  
  33.     [smsButton setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateNormal];  
  34.     [smsButton addTarget:self action:@selector(displayComposerSheet) forControlEvents:UIControlEventTouchUpInside];  
  35.     message = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 360.0, 280.0, 29.0)];  
  36.     message.frame = CGRectMake(20.0, 360.0, 280.0, 29.0);  
  37.     message.adjustsFontSizeToFitWidth = YES;  
  38.     message.hidden = YES;  
  39.     message.text = @"";  
  40.     message.userInteractionEnabled = NO;  
  41.     [self.view addSubview:smsButton];  
  42.     [self.view addSubview:message];  
  43. }  
  44. -(void)displayComposerSheet  
  45. {  
  46.     MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];  
  47.     icker.messageComposeDelegate = self;  
  48.     picker.recipients = [NSArray arrayWithObject:@"123456789"];  
  49.     // your recipient number or self for testing  
  50.     picker.body = @"test from OS4";  
  51.     [self presentModalViewController:picker animated:YES];  
  52.     [picker release];  
  53.     NSLog(@"SMS fired");  
  54. }  
  55. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
  56. {  
  57.     message.hidden = NO;/  
  58.     switch (result)  
  59.     {  
  60.         case MessageComposeResultCancelled:  
  61.             message.text = @"Result: canceled";  
  62.             NSLog(@"Result: canceled");  
  63.             break;  
  64.         case MessageComposeResultSent:  
  65.             message.text = @"Result: sent";  
  66.             NSLog(@"Result: sent");  
  67.             break;  
  68.         case MessageComposeResultFailed:  
  69.             message.text = @"Result: failed";  
  70.             NSLog(@"Result: failed");  
  71.             break;  
  72.         default:  
  73.             message.text = @"Result: not sent";  
  74.             NSLog(@"Result: not sent");  
  75.             break;  
  76.      }  
  77.      [self dismissModalViewControllerAnimated:YES];  
  78. }  
  79. // Override to allow orientations other than the default portrait orientation.  
  80. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  81. {  
  82.     return YES;  
  83. }  
  84. - (void)didReceiveMemoryWarning  
  85. {  
  86.     // Releases the view if it doesn't have a superview.  
  87.     [super didReceiveMemoryWarning];  
  88.     // Release any cached data, images, etc that aren't in use.  
  89. }  
  90. - (void)viewDidUnload  
  91. {  
  92.     // Release any retained subviews of the main view.  
  93.     // e.g. self.myOutlet = nil;  
  94. }  
  95. - (void)dealloc  
  96. {  
  97.     [super dealloc];  
  98. }  
  99. @end  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值