发送指内容的短信
#import "ViewController.h"
#import <MessageUI/MessageUI.h>
@interface ViewController ()<MFMessageComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = [NSArray arrayWithObject:@"13888888888"];//指定发送人
controller.body = @"发送短信验证";//指定发送内容
controller.messageComposeDelegate = self;//设置代理
[self presentViewController:controller animated:YES completion:^{
//修改短信界面标题
[[[[controller viewControllers]lastObject]navigationItem ]setTitle:@"修改短信界面"];
}];
}else{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"该设备不支持发送" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"acton" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
return ;
}];
[alertVC addAction:action];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:NO completion:nil];
switch ( result ) {
case MessageComposeResultCancelled:
{
//click cancel button
NSLog(@"取消发送");
}
break;
case MessageComposeResultFailed:// send failed
{
NSLog(@"发送失败");
}
break;
case MessageComposeResultSent:
{
//do something
NSLog(@"已发送出去了");
}
break;
default:
break;
}
}
@end