ios - 如何在 iOS 应用程序中发送带附件(文件 PDF)的电子邮件?

标签 ios objective-c mfmailcomposeviewcontroller

我的应用程序中有按钮,当我打开时,它会显示带有附件的新电子邮件。在我向我的电子邮件地址发送电子邮件后,我收到了这条消息和附件,但附件包含我应用程序的最后一个屏幕。我想用支持文件中的附件打开这条消息。也许你知道我哪里会出错?

- (IBAction)showEmail:(id)sender {

NSString *emailTitle =  @"elllo";

NSString *messageBody = @"Hi ! \n Below I send you ";

NSArray *toRecipents = [NSArray arrayWithObject:@"m1891@gmail.com"];

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIGraphicsEndPDFContext();
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

mc.mailComposeDelegate = self;

[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"MV.pdf"];
[mc setToRecipients:toRecipents];

[self presentViewController:mc animated:YES completion:NULL];

最佳答案

下面是正确的解决方案:

 - (IBAction)showEmail:(id)sender {

    NSString *emailTitle =  @"elllo";

    NSString *messageBody = @"Hi ! \n Below I send you ";

    NSArray *toRecipents = [NSArray arrayWithObject:@"m1891@gmail.com"];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MV" ofType:@"pdf"]; NSData *myData
= [NSData dataWithContentsOfFile: path];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"MV.pdf"];

    [mc setToRecipients:toRecipents];

    [self presentViewController:mc animated:YES completion:NULL];

}

关于ios - 如何在 iOS 应用程序中发送带附件(文件 PDF)的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421105/

相关文章:

iphone - MPMoviePlayerViewController 不播放下载的文件

ios - 运行UIImagePickerController Xcode 7时模拟器崩溃

ios - UISearchBar 出现在 UITableView 的*底部*

ios - MFMailComposer 以不同方向附加屏幕截图图像时无法正确显示

ios - Branch 打不开 App Store 链接

iphone - 如何检查互联网是否活跃并且工作正常?

ios - webView 中的本地 pdf 文件

ios , 将音频文件 aac 格式附加到电子邮件

ios - iOS9 MFMailComposeViewController 的 NavigationBar 中的自定义字体?

ios - Xcode 方法自动完成仅显示某些类