添加一個pdf到Email中,操作如下:
-(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");
}
}
設置數據:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"favorite.pdf"];
myData = [NSData dataWithContentsOfFile: filePath];
在看log mydata(就是pdf)時,顯示並不為空。浏覽模擬器“我的文檔”也能看見pdf。
請指教為什麼我的pdf無法添加到Email中?
在ios6中沒問題,只有在ios5中有問題。
你先顯示了view controller,然後才將文件作為附件添加,因此修改一下操作順序就好。
這行代碼:
[self presentModalViewController:controller animated:YES];
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
修改為:
[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
[self presentModalViewController:controller animated:YES];