问题
I tried to attach my pdf to an in-app-mail. The in-app-mail displays an icon with the pdf but it doesn't send it. I don't know why...
Here's the code:
- (void)openInEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.mailComposeDelegate = self;
[viewController setSubject:@"Stundenplan 1A"];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];
[viewController addAttachmentData:data mimeType:@"text/pdf" fileName:@"Stundenplan_1A.pdf"];
[self presentModalViewController:viewController animated:YES]; }
}
Any ideas?
回答1:
Have you tried it with?
NSString *filePath = [documentsDirectory stringByAppendingFileComponent:@"%@/Stundenplan_1A.pdf"];
instead of
NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];
And instead of NSMutableData you could tried it with NSData
NSData *data = [NSData dataWithContentsOfFile:file];
回答2:
Change the mime type like this.
NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"CheckList.pdf"];
来源:https://stackoverflow.com/questions/9101262/attach-a-pdf-file-in-app-mail