0

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];
iOSAaronDavid
  • 140
  • 15
  • Make sure `myData` isn't `nil`. – rmaddy Jun 09 '14 at 15:56
  • Why do you call `dismissViewController` immediately after calling `presentViewController`? That makes no sense. – rmaddy Jun 09 '14 at 15:57
  • MFMailComposeViewController is something from Apple inside of Xcode. Anyone can use it. The composer is dismissed after the user is done with it. myData is not nil, it has something. (NSObject) NSObject = { isa = NSConcreteData } – iOSAaronDavid Jun 09 '14 at 16:04
  • I know what `MFMailComposeViewController`. That's not relevant to my question. I asked why you call `dismissViewController` immediately after calling `presentViewController`. You shouldn't do that. You need to dismiss the mail composer inside the delegate method. – rmaddy Jun 09 '14 at 16:06
  • I had other problems in other parts of the code, that is why it was not working. I removed [self dismissViewControllerAnimated:YES completion:nil]; and it is still able to dismiss when I click cancel in the ComposeViewController. Thanks for the advice, I appreciate it. – iOSAaronDavid Jun 09 '14 at 17:58

1 Answers1

0

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

George
  • 11
  • 3
user3246173
  • 488
  • 6
  • 18