【MessageUI】iOS调用 短信 与 邮件
2016-06-27 本文已影响265人
居然是村长
虽然发短信和发邮件都可以使用 openURL去处理,但使用MessageUI可以做到回调成功失败,而且也非常简单;
发邮件
- (void)viewDidLoad {
[super viewDidLoad];
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"主题"];
[controller setToRecipients:@[@"shoujianren@qq.com"]];
[controller setCcRecipients:@[@"chaosong@qq.com"]];
[controller setBccRecipients:@[@"misong@qq.com"]];
[controller setMessageBody:@"啥" isHTML:NO];
NSString *path = [[NSBundle mainBundle] pathForResource:@"bg1" ofType:@"png"];
[controller addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:@"png" fileName:@"附件"];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"取消");
break;
case MFMailComposeResultSaved:
NSLog(@"已保存");
break;
case MFMailComposeResultSent:
NSLog(@"已发送");
break;
case MFMailComposeResultFailed:
NSLog(@"发送失败");
break;
default:
break;
}
}
发短信
- (void)viewDidLoad {
[super viewDidLoad];
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.messageComposeDelegate = self;
controller.recipients = @[@"187********"];
controller.body = @"晚饭吃了吗?";
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"取消");
break;
case MessageComposeResultSent:
NSLog(@"已发送");
break;
case MessageComposeResultFailed:
NSLog(@"发送失败");
break;
default:
break;
}
}
1