iOS 共享到Mail邮件

iOS 共享爬坑记

神坑

特别坑

利用系统的Mail邮件发送,刚开始收不到,还以为是我的代码有问题,检查了几遍,没啥问题啊。然后检验邮箱设置,好像也没啥问题啊?啊?
等回家之后,开启**,然后重新发送,尝试一下,然后……成功了!?
这么玩的么?

废话不多说,直接开始吧。

// Mail
#import 

- (void)emailAction {
    [self sendMailWithFilePath:[[NSBundle mainBundle] pathForResource:@"4.pdf" ofType:nil]];
}

发送邮件

- (void)sendMailWithFilePath:(NSString *)filePath {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc]init];
        mailController.mailComposeDelegate = self;
        // 标题
        [mailController setSubject:[filePath lastPathComponent]];
        // 收件箱
        [mailController setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
        // 内容
        [mailController setMessageBody:@"This is content" isHTML:NO];

        // 附件
        NSData *Data = [NSData dataWithContentsOfFile:filePath];
        //发送文件的NSData,类型,附件名,根据实际x情况进行修改
        [mailController addAttachmentData:Data mimeType:@"application/pdf" fileName:[filePath lastPathComponent]];

        [self presentViewController:mailController animated:YES completion:nil];
    }else {
        NSLog(@"Your iPhone can't send Mail");
    }
}

MFMailComposeViewControllerDelegate

#pragma mark MFMailComposeViewControllerDelegate

 - (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error
{
    NSString *resultString = nil;
    switch (result){
        case MFMailComposeResultCancelled:
            resultString = [NSString stringWithFormat:@"Mail send canceled…"];
            break;
        case MFMailComposeResultSaved:
            resultString = [NSString stringWithFormat:@"Mail saved…"];
            break;
        case MFMailComposeResultSent:
            resultString = [NSString stringWithFormat:@"Mail sent out…"];
            break;
        case MFMailComposeResultFailed:
            resultString = [NSString stringWithFormat:@"%@", [error localizedDescription]];
            break;
        default:
            break;
    }
    NSLog(@"%@",resultString);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:resultString delegate:self cancelButtonTitle:@"取消"otherButtonTitles:nil];
    [alert show];
    [self dismissViewControllerAnimated:YES completion:nil];
}

在这里,本来想实现一个操作,发送邮件给自己。那么需要获取用户的邮箱,但是找了一圈,暂时没有发现获取自己邮箱的方法。
之前有看到说通过account的plist文件获取,发现是空的;通过ACAccountStore的accounts获取,一样是空数组。因此这个暂时就先停在这里了。

由于做共享有一段时间了,确实挺疲惫的,暂时就先告一段落了。

你可能感兴趣的:(iOS 共享到Mail邮件)