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

时间:2016-04-05 08:34:28

标签: ios objective-c mfmailcomposeviewcontroller

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

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

}

1 个答案:

答案 0 :(得分:9)

下面是正确的溶剂:

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

}