IOS使用MessageUI Framework 发送短信息
使用MessageUI Framework 中的 MFMessageComposeViewController 发送短信息,另外其中的MFMailComposeViewController用于发送邮件。使用非常简单:
1. 导入框架:MessageUI.framework
2. 添加协议:<MFMessageComposeViewControllerDelegate>
3. #import <MessageUI/MessageUI.h>
1 //判断当前设备是否可以发送短信息 2 if([MFMessageComposeViewController canSendText]) 3 { 4 5 MFMessageComposeViewController *mc = [[MFMessageComposeViewController alloc] init]; 6 //设置委托 7 mc.messageComposeDelegate = self; 8 //短信内容 9 mc.body = @"nihao"; 10 //短信接收者,可设置多个 11 mc.recipients = [NSArray arrayWithObject:@"10086",nil]; 12 13 [self presentModalViewController:mc animated:YES]; 14 [mc release]; 15 } 16 else 17 { 18 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error", 19 message:@"The current device can not send SMS" 20 delegate:nil 21 cancelButtonTitle:"OK", 22 otherButtonTitles:nil]; 23 [alert show]; 24 [alert release]; 25 } 26 27 28 #pragma mark - 29 #pragma mark MFMessageComposeViewControllerDelegate 30 -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 31 { 32 switch(result) 33 { 34 case MessageComposeResultCancelled: 35 ... 36 case MessageComposeResultFailed: 37 ... 38 case MessageComposeResultSend: 39 ... 40 default: 41 ... 42 } 43 }