2016-04-05 28 views
1

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

- (IBAction)showEmail:(id)sender { 

NSString *emailTitle = @"elllo"; 

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

NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 

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

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];意味着你正在制作一个截图,绘制实际的PDF本身 – ogres

+0

OK谢谢@ogres!如何从支持文件发送附件(文件pdf)的电子邮件? – mechu911

+0

您可以先将文件读取为NSData,然后附上该数据 – ogres

回答

6

下面它的正确sollution:

- (IBAction)showEmail:(id)sender { 

    NSString *emailTitle = @"elllo"; 

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

    NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 

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

} 
+0

很好的答案!就是这个! –

相关问题