使用MessageUI Framework 中的 MFMessageComposeViewController 发送短信息,另外其中的MFMailComposeViewController用于发送邮件。使用非常简单:
1. 导入框架:MessageUI.framework 2. 添加协议:<MFMessageComposeViewControllerDelegate> 3. #import <MessageUI/MessageUI.h>//判断当前设备是否可以发送短信息
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *mc = [[MFMessageComposeViewController alloc] init];
//设置委托
mc.messageComposeDelegate = self;
//短信内容
mc.body = @"nihao";
//短信接收者,可设置多个
mc.recipients = [NSArray arrayWithObject:@"10086",nil];
[self presentModalViewController:mc animated:YES];
[mc release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error",
message:@"The current device can not send SMS"
delegate:nil
cancelButtonTitle:"OK",
otherButtonTitles:nil];
[alert show];
[alert release];
}
#pragma mark -
#pragma mark MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch(result)
{
case MessageComposeResultCancelled:
...
case MessageComposeResultFailed:
...
case MessageComposeResultSend:
...
default:
...
}
}