Place PDF in NSData

How to put my PDF in NSData ? I have a PDF layout as a string in my Application Document Reference. When I try to send it by e-mail, I see the PDF in the body of the letter (see. Application icon. I do not know if this is normal or not). But when I receive email on the other end, it does not have a PDF extension. What am I doing wrong? Thanks.

 NSString *documentsDirectory = [self GetURLForPDF]; // I know this name is bad since it is really a NSString NSLog(@"DocumentsDirectory: %@", documentsDirectory); NSString *pdfName = [NSString stringWithFormat:@"%@/%@.pdf", documentsDirectory, title]; NSLog(@"pdfName: %@", pdfName); NSData *pdfData = [NSData dataWithContentsOfFile:pdfName]; [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:title]; [self presentModalViewController:mailComposer animated:YES]; 
+6
source share
1 answer

Depending on how the pdfName variable changes, it looks like your "title" value does not include the PDF suffix. You tried:

 NSString *documentsDirectory = [self GetURLForPDF]; // I know this name is bad since it is really a NSString NSLog(@"DocumentsDirectory: %@", documentsDirectory); NSString *fullTitle = [NSString stringWithFormat:@"%@.pdf", title]; NSString *pdfName = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fullTitle]; NSLog(@"pdfName: %@", pdfName); NSData *pdfData = [NSData dataWithContentsOfFile:pdfName]; [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:fullTitle]; [self presentModalViewController:mailComposer animated:YES]; 
+9
source

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


All Articles