Search code examples
iosobjective-cmfmailcomposeviewcontrollermfmailcomposer

How do I email a pdf using the MFMailComposeViewContoller?


I have a pdf file called FlashCards.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];

The email is being sent and it seems that everything is working, but it's not sending the PDF file.

EDIT:

This actually works, I had other problems in other parts of my code. My problem is solved. I also made sure that the extensions were correct instead of having FlashCards as a file name, it should be FlashCards.pdf This is the exact code I have working:

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

Solution

  • Use MIME type text/x-pdf instead of application/pdf. Also check the size/length of myData to verify that the PDF was loaded.