【MessageUI】iOS调用 短信 与 邮件

作者: 居然是村长 | 来源:发表于2016-06-27 22:00 被阅读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

    相关文章

      网友评论

        本文标题:【MessageUI】iOS调用 短信 与 邮件

        本文链接:https://www.haomeiwen.com/subject/amzzdttx.html