I am trying to attach an image and a PDF to an email using the MessageUI framework. I followed the MailComposer example in the Apple documentation.
On the iPhone, it seems to be working fine, the image and the PDF are both displayed in the body of the sending mailbox as expected.
However, when I receive an email on my MacBook, there are two problems.
1) myImage.png is displayed as an attachment and is the correct size but completely empty
2) myPDF.pdf does not appear as an attachment at all
But, when I receive mail on my iPhone, myImage.png displays well. myPDF.pdf still does not appear in the mail on my iPhone.
Hope someone can shed light on what might happen.
Here is the relevant code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Test Email"]; // Attach an image to the email NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"]; NSData *imageData = [NSData dataWithContentsOfFile:imagePath]; [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"myImage"]; // Attach a PDF file to the email NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"]; NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath]; [picker addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"myPDF"]; // Fill out the email body text NSString *emailBody = @"This is a test."; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker release];
EDIT Instead of saving and extracting the image and PDF to my main unit, I used NSDocumentsDirectory and everything worked fine.
source share