Can't send pdf file with xcode email address

Please help me! I can send the image, but I can not send the PDF file, despite the fact that I see the attached icon file in the email. My code is:

MFMailComposeViewController *sendmailview = [[MFMailComposeViewController alloc] init]; sendmailview.mailComposeDelegate = self; // I can send image with: // UIImage *roboPic = [UIImage imageNamed:@"Default.png"]; // NSData *imageData = UIImageJPEGRepresentation(roboPic, 1); // [sendmailview addAttachmentData:imageData mimeType:@"image/png" fileName:@"Default.png"]; // But Can't send pdf file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *file = [documentsDirectory stringByAppendingFormat:@"book.pdf"]; NSMutableData *data=[NSMutableData dataWithContentsOfFile:file]; [sendmailview addAttachmentData:data mimeType:@"application/pdf" fileName:@"book.pdf"]; // I see icon book.pdf when attach,but when i sent mail,mail receiver not see pdf file... [self presentModalViewController:sendmailview animated:YES]; [sendmailview release]; 

Help me........

Thanks!

+6
source share
1 answer

Use

 NSString *file = [documentsDirectory stringByAppendingFileComponent:@"book.pdf"]; 

instead

 NSString *file = [documentsDirectory stringByAppendingFormat:@"book.pdf"]; 

In addition, the data may be NSData instead of NSMutableData .

 NSData *data = [NSData dataWithContentsOfFile:file]; 

Finally, have you ensured that the PDF file is present in the path you are linking to? Is the variable data valid?

+4
source

Source: https://habr.com/ru/post/899580/


All Articles