【问题标题】:How do I email a pdf using the MFMailComposeViewContoller?如何使用 MFMailComposeViewContoller 通过电子邮件发送 pdf?
【发布时间】:2014-06-09 15:48:55
【问题描述】:

我有一个名为 FlashCards.pdf 的 pdf 文件

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

NSArray *recipients = @[@"aarone_2010@hotmail.com"];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"FlashCards" ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"application/pdf" fileName:@"FlashCards"];
[picker setSubject:@"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:@"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

正在发送电子邮件,似乎一切正常,但没有发送 PDF 文件。

编辑:

这确实有效,我在代码的其他部分遇到了其他问题。我的问题解决了。我还确保扩展名是正确的,而不是使用 FlashCards 作为文件名,它应该是 FlashCards.pdf 这是我工作的确切代码:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *recipients = @[@"android.aaron.david@gmail.com"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"FlashCards" ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"application/pdf" fileName:@"FlashCards.pdf"];
[picker setSubject:@"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:@"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];

【问题讨论】:

  • 确保myData 不是nil
  • 为什么在调用presentViewController 后立即调用dismissViewController?这没有任何意义。
  • MFMailComposeViewController 是苹果在 Xcode 内部的东西。任何人都可以使用它。用户完成后,作曲家将被解雇。 myData 不是零,它有一些东西。 (NSObject) NSObject = { isa = NSConcreteData }
  • 我知道MFMailComposeViewController。这与我的问题无关。我问你为什么在拨打presentViewController 后立即拨打dismissViewController。你不应该那样做。您需要在委托方法中关闭邮件编写器。
  • 我在代码的其他部分遇到了其他问题,这就是它无法正常工作的原因。我删除了 [self dismissViewControllerAnimated:YES completion:nil];当我在 ComposeViewController 中单击取消时,它仍然可以关闭。感谢您的建议,我很感激。

标签: ios objective-c mfmailcomposeviewcontroller mfmailcomposer


【解决方案1】:

使用 MIME 类型 text/x-pdf 而不是 application/pdf。还要检查 myData 的大小/长度,以验证 PDF 是否已加载。

【讨论】:

    猜你喜欢
    • 2018-09-19
    • 2023-03-06
    • 2015-06-21
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多