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

How to send email with attachment(file PDF) in app iOS?

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

- (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];

}