iOS开发之---发送邮件

通过邮件视图控制器封装了邮件发送功能,开发者不需要知道不同的邮件协议或者如何与邮件服务器连接建立等。在iOS中,这些功能都被打包好,通过

MFMailComposeViewController提供给我们。

引入MessageUI库。

搭建UI界面,添加按钮的事件响应方法:

- (IBAction)demoAction:(id)sender {
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"请选择任务" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"打开邮件" otherButtonTitles:@"打开微博",@"打开微信", nil];
    [action showInView:self.view];
}

效果:

动作表单代理方法:

#pragma mark - UIActionSheet代理方法
// 处理动作表单点击事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        mailVC.mailComposeDelegate = self;
        [mailVC setSubject:@"发送测试邮件"];
        [mailVC setMessageBody:@"测试内容" isHTML:NO];
        [self presentViewController:mailVC animated:YES completion:nil];
    }
}

效果:

处理邮件发送的代理方法:

#pragma mark - MFMailComposeViewControllerDelegate方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"取消发送邮件");
            [controller dismissViewControllerAnimated:YES completion:nil];
            break;
            
        case MFMailComposeResultSent:
            NSLog(@"邮件发送成功");
            [controller dismissViewControllerAnimated:YES completion:nil];
            break;
        case MFMailComposeResultFailed:
            NSLog(@"邮件发送失败");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"邮件成功保存");
            [controller dismissViewControllerAnimated:YES completion:nil];
        default:
            break;
    }
}

点击发送按钮:

 

附注:

modal展示:presentViewController: animated: completion: 对应dismissViewControllerAnimated: completion:

push展示:push....对pop.....

 

posted @ 2014-05-14 19:39  Big.Eagle  阅读(464)  评论(0编辑  收藏  举报