Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put PDF into NSData

Tags:

iphone

How do I put my PDF into NSData? I have the location of the PDF as a string in my Documents Directory of the app. When I try to email it, I see the PDF in the body of the email (vs seeing an attachment icon. I don't know if that's normal or not). But when I receive the 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];
like image 337
Crystal Avatar asked Sep 23 '11 22:09

Crystal


1 Answers

Based on how the pdfName variable is being set, it looks like your "title" value does not include the PDF suffix. Have 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];
like image 95
Tim Dean Avatar answered Sep 20 '22 14:09

Tim Dean