【问题标题】:How to send email with attachment(file PDF) in app iOS?如何在应用程序 iOS 中发送带有附件(文件 PDF)的电子邮件?
【发布时间】:2016-04-05 08:34:28
【问题描述】:

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

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

}

【问题讨论】:

  • [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];表示您正在制作屏幕截图,绘制实际的 PDF 本身
  • 好的,谢谢@ogres!如何从支持文件发送带有附件(文件 pdf)的电子邮件?
  • 您可以先将文件读取为 NSData,然后附加该数据

标签: ios objective-c mfmailcomposeviewcontroller


【解决方案1】:

下面是正确的解决方案:

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

}

【讨论】:

猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 2016-10-08
  • 2016-09-08
  • 2020-11-20
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多