IOS 发送Email的方法 兼容iPad iPhone

IOS 发送Email的方法 兼容iPad iPhone

//调出邮件发送窗口
- (void)displayMailPicker
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: @"图片分享"];
    //添加收件人
//    NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];
//    [mailPicker setToRecipients: toRecipients];
    //添加抄送
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
//    [mailPicker setCcRecipients:ccRecipients];
    //添加密送
//    NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
//    [mailPicker setBccRecipients:bccRecipients];
    
    NSArray *tempExport = self.selectedTag == SelectedAllTag ? self.fileInfoArray : self.selectedFileInfoArray;
    for (FileInfo *fileInfo in tempExport) {
        // 添加一张图片
        UIImage *addPic = [UIImage imageWithContentsOfFile:fileInfo.path];
        NSData *imageData = UIImagePNGRepresentation(addPic);            // png
        //关于mimeType:http://www.iana.org/assignments/media-types/index.html
        [mailPicker addAttachmentData: imageData mimeType: @"" fileName: fileInfo.name];
    }

    
    //添加一个pdf附件
//    NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
//    NSData *pdf = [NSData dataWithContentsOfFile:file];
//    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
    
//    NSString *emailBody = @"<font color='red'>eMail</font> 正文";
//    [mailPicker setMessageBody:emailBody isHTML:YES];
    //[self.navigationController pushViewController:mailPicker animated:YES];
    [self presentViewController:mailPicker animated:YES completion:^{
        [HUDUtil removeHUD];
    }];
}


/**
 *  邮件代理
 *
 *  @param controller <#controller description#>
 *  @param result     <#result description#>
 *  @param error      <#error description#>
 */
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0)
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:^{
        [self.navigationController popViewControllerAnimated:NO];
    }];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"用户成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"用户点击发送,将邮件放到队列中,还没发送";
            break;
        case MFMailComposeResultFailed:
            msg = @"用户试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
}

猜你喜欢

转载自huqiji.iteye.com/blog/2187034