Put PDF into NSData - 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];

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];

Related

Save PDF then send in email

I am trying to save a pdf from a url and then send it in an email. The sender seems to have it but the receiver does not get it.
When I do a NSLog of the NSString file I get /var/mobile/Applications/0ADE222E-6346-4C6C-8348-DA5327B980AA/Documents/myPDF.pdf
It seems like it saves but it doesn't send. Here is my code below for saving and sending
EDIT
Updated code
// This pdfURL is 0 bytes
NSData *pdfURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",webPage.urlString]]];
//Store the Data locally as PDF File
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *file = [documentDirectory stringByAppendingFormat:#"/myPDF.pdf"];
[pdfURL writeToFile:file atomically:YES];
//Sending the pdf
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]){
//Changed email for privacy issues
[composer setToRecipients:[NSArray arrayWithObjects:#"123#abc.com", nil]];
[composer setSubject:[NSString stringWithFormat:#"%# email",titleText]];
[composer setMessageBody:#"your custom body content" isHTML:NO];
NSLog(#"pdf %#",file);
// This pdfData is 0 bytes
NSData *pdfData = [NSData dataWithContentsOfFile:file];
[composer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"myPDF.pdf"];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
If it's from a URL, store it in NSData:
NSData *pdfData = [NSData dataWithContentsOfURL[NSURL URLWithString:#"URL Of Pdf"]];
And then attach that to mail
The issue was with the webPage.urlString being empty. This code works just make sure the url string is not empty :P

how to create and attach CSV file in MFMailComposer in iPhone sdk?

i have created one csv file and i am also attaching it to MFMailComposer and it shows me to my mail composer but when i send it to user email it does not shows me attached csv file in email. i have used this code to create csv file and adding data in it.
NSMutableString *mainString=[[NSMutableString alloc]initWithString:#""];
//NSMutableArray *section = [[NSMutableArray alloc] init];
for(int i = 0;i<[NameArray count];i++)
{
NSString *string=[indexArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"\"%#\"",string];
string=[NameArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
string=[typearray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#"\n"];
}
NSLog(#"getdatafor csv:%#",mainString);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"history.csv"];
// filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
NSError *error;
[settingsData writeToFile:filePath atomically:YES];
// NSLog(#"writeok");
NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error];
NSLog(#"Length:%d Error:%#",[mediaData length],[error localizedDescription]);
here the above code is working good i am getting [mediaData length] i am attaching the CSV file from here.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"history" ofType:#"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];
// Fill out the email body text
NSString *emailBody = #"history";
[picker setMessageBody:emailBody isHTML:NO];
[picker addAttachmentData:myData mimeType:#"text/cvs" fileName:#"history"];
[self presentModalViewController:picker animated:YES];
[picker release];
the above code is also working properly. it shows me attached CSV file but when i am sending the mail by email at that time receiver is not getting the attached CSV file. what's wrong in this code.? why the receiver is not getting the attached file.?
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"CSV File"];
NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];
[mailer addAttachmentData:myData mimeType:#"text/cvs" fileName:#"FileName"];
[self presentModalViewController:mailer animated:YES];
Where the 'text' is a string.
i have solved this issue with attaching files and other media property in MFMailComposeViewController.
NSData *data=[[arr componentsJoinedByString:#","] writeToFile:#"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:#"text/csv" fileName:#"Bhavesh.csv"];

Attach a pdf file in-app-mail

I tried to attach my pdf to an in-app-mail. The in-app-mail displays an icon with the pdf but it doesn't send it. I don't know why...
Here's the code:
- (void)openInEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.mailComposeDelegate = self;
[viewController setSubject:#"Stundenplan 1A"];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];
[viewController addAttachmentData:data mimeType:#"text/pdf" fileName:#"Stundenplan_1A.pdf"];
[self presentModalViewController:viewController animated:YES]; }
}
Any ideas?
Have you tried it with?
NSString *filePath = [documentsDirectory stringByAppendingFileComponent:#"%#/Stundenplan_1A.pdf"];
instead of
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
And instead of NSMutableData you could tried it with NSData
NSData *data = [NSData dataWithContentsOfFile:file];
Change the mime type like this.
NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"CheckList.pdf"];

How do i send a saved text file as an email attachment

I have an app which is sending data collected via an email as an attachment. I use the following code to create and save the data to a file called 'leads.xml' in my docs folder.
NSArray *sysPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *sysFilePath = [sysPath objectAtIndex:0];
[picker addAttachmentData:sysFilePath mimeType:#"text/xml" fileName:#"leads.xml"];
.
.
.
[emailBody writeToFile:filePath atomically:YES];
I then try to send the saved file as an attachment using this code -
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/leads.xml", docDirectory];
When it runs I get an error, what am I doing wrong?
The mail want a NSData object, contain the files content not a path.
You will need to load the file into a NSData object like:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingPathComponent:#"leads.xml"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
[picker addAttachmentData:fileData mimeType:#"text/xml" fileName:#"leads.xml"];
You have to supply the data, not a file path:
NSData *data = [NSData dataWithContentsOfFile:sysFilePath];
[picker addAttachmentData:data mimeType:#"text/xml" fileName:fileName];
Docs:
- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
attachment:
The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil.
Try this code
if(mailController)
{
mailController.mailComposeDelegate = self;
// NSLog(#"current index:%d",currentIndex_);
NSString *string = [NSString stringWithFormat:#"00%d",currentIndex_+43];
// NSLog(#"string:%#",string);
NSString *imageIndex = [[NSBundle mainBundle] pathForResource:string ofType:#"jpg"];
NSData *myData = [[NSData alloc] initWithContentsOfFile:imageIndex];
[mailController addAttachmentData:myData mimeType:#"application/jpg" fileName:#"Report"];
[mailController setSubject:#"your subject"];
[mailController setMessageBody:#"" isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
I am sending image here instead of it you can send your text file.
This may help you
Couple of things...
You can check the file system for the simulator at:
~/Library/Application Support/iPhone Simulator/5.0/Applications/...
and see if your file is actually being saved where you expect.
Second thing:
NSString actually has a load of convenience methods for working with file paths so you don't have to check for trailing slashes and other junk. You should always use them when working with file paths.
Working with Paths
+ pathWithComponents:
– pathComponents
– completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
– fileSystemRepresentation
– getFileSystemRepresentation:maxLength:
– isAbsolutePath
– lastPathComponent
– pathExtension
– stringByAbbreviatingWithTildeInPath
– stringByAppendingPathComponent:
– stringByAppendingPathExtension:
– stringByDeletingLastPathComponent
– stringByDeletingPathExtension
– stringByExpandingTildeInPath
– stringByResolvingSymlinksInPath
– stringByStandardizingPath
– stringsByAppendingPaths:

writeToFile always crashes with stringByAppendingPathComponent

I am sitting on this problem for hours and I am blind now. ;-)
I have this code here:
- (id) initWithImage:(UIImageView*)imageView andPath: (NSString*) path{
[super init];
drawImage = imageView;
imageFilePath = [NSString stringWithString: path];
NSLog(#"fath: %#", imageFilePath);
return self;
}
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(drawImage.image)];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:#"img.data"];
NSLog(#"path: %#", path);
[imageData writeToFile:path atomically:YES];
This code works but when i change
[imageData writeToFile:path atomically:YES];
to
[imageData writeToFile:imageFilePath atomically:YES];
the app crashes.
But the strings look completely the same:
2011-06-01 10:38:10.178 L3T[3756:207] fath: /Users/max/Library/Application Support/iPhone Simulator/4.3/Applications/A73945CF-9FD0-46E9-A16B-9C0EFC924B0F/Documents/img.data
2011-06-01 10:38:11.864 L3T[3756:207] path: /Users/max/Library/Application Support/iPhone Simulator/4.3/Applications/A73945CF-9FD0-46E9-A16B-9C0EFC924B0F/Documents/img.data
I just don't understand it!
The code imageFilePath = [NSString stringWithString: path]; gives you an object that you do not own, hence you cannot depend on it being available later in your code. Change this to:
imageFilePath = [[NSString alloc] initWithString: path];
and you should be fine.