I am trying to add an email as an attachment to my mail. I do it like this.
-(IBAction)mailPDF:(id)sender{
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSLog(@"myData is %@",myData);
[controller setSubject:@"Geselecteerde favorieten van Genk on Stage"];
[controller setMessageBody:@"<p>Hallo muziekliefhebber <br /> In bijlage vind je jouw favorieten. Volg en praat met ons mee op facebook.com/genkonstage of @genkonstage!<br /> Veel plezier op Genk on stage! </p>" isHTML:YES];
if (controller){
[self presentModalViewController:controller animated:YES];
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
}else{
NSLog(@"nothing to show");
}
}
This is how I set myData
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"favorite.pdf"];
myData = [NSData dataWithContentsOfFile: filePath];
When I look at my log myData
(which is my pdf) it is NOT empty. Also when I browse in the finder to my documents folder of the simulator
I see that I have a PDF.
Can someone tell me why my pdf is not added to my mail ?
thank you!
EDIT It seems that above code only works in IOS6. So the question is now. Why is it not working in IOS 5
You are presenting the view controller first, and then attaching the file. Change the sequence of actions:
That is, In your lines of code:
[self presentModalViewController:controller animated:YES];
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
Modify them as:
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
[self presentModalViewController:controller animated:YES];
Hope it will resolve the issue :)
try this,
-(IBAction)mailPDF:(id)sender{
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSLog(@"myData is %@",myData);
[controller setSubject:@"Geselecteerde favorieten van Genk on Stage"];
[controller setMessageBody:@"<p>Hallo muziekliefhebber <br /> In bijlage vind je jouw favorieten. Volg en praat met ons mee op facebook.com/genkonstage of @genkonstage!<br /> Veel plezier op Genk on stage! </p>" isHTML:YES];
if (controller)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"favorite.pdf"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
[self presentModalViewController:controller animated:YES];
}
else{
NSLog(@"nothing to show");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.