【发布时间】: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