if( [MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = @[@"10086"];//发送短信的号码,数组形式入参
controller.navigationBar.tintColor = [UIColor redColor];
controller.body = @"body"; //此处的body就是短信将要发生的内容
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"title"];//修改短信界面标题
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
message:@"该设备不支持短信功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
}
首先是判断手机是否支持调取短信功能,头文件引入#import <MessageUI/MessageUI.h>如要获取发送状态,遵守代理
MFMessageComposeViewControllerDelegate
并实现代理方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
//信息传送成功
break;
case MessageComposeResultFailed:
//信息传送失败
break;
case MessageComposeResultCancelled:
//信息被用户取消传送
break;
default:
break;
}
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }