So I have had this problem for sometime and just cant get it working! I have been building a survey app that users simply enter information in and its saved to a csv file. Im now at the stage where I need to attached the csv file within the app to an email address...
I just tested this on my new i-phone and there is no attachment when the email is received? Its there in the mail app and in the simulator, however when the message is received on the email account the attachment has gone? Can anyone help?? My code is below:
- (IBAction)send:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:#"result‌s.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
[mailcomposer addAttachmentData:csvData mimeType:#"text/csv" fileName:#"results.csv"];
[mailcomposer setToRecipients:#[#"gpsflighttrial#gcap.eu"]];
[mailcomposer setSubject:self.subject.text];
[mailcomposer setMessageBody:self.message.text isHTML:NO];
}
Change your mimeType to #"application/csv" and it will work.
Reason for csv not being attached can be ether of below :
1) filename or filepath is wrong as result nsdata of csv will be nil.
2) filename in addAttachmentData mimeType: fileName: method should cantain only name not type of file format.
Changes in your method is given below :
-(IBAction)send:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Determine the file name and extension
NSString *strFileName = #"results.csv";
NSArray *filepart = [strFileName componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
//get file path
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName];
//Now check if file exits
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:savedFilePath]){
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];
if(csvData)
{
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
[mailcomposer addAttachmentData:csvData mimeType:#"text/csv" fileName:filename];
[mailcomposer setToRecipients:#[#"gpsflighttrial#gcap.eu"]];
[mailcomposer setSubject:self.subject.text];
[mailcomposer setMessageBody:self.message.text isHTML:NO];
}
else
NSLog(#"error csv data not created");
}
else
NSLog(#"error file doesnot exists");
}
So after many sleepless nights and a lot of trying different ways I finally found the way that works with a friend (see code below)
NSString *docsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSData *attachment = [NSData dataWithContentsOfFile:[docsDirectory stringByAppendingPathComponent:#"results.csv"]];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setToRecipients:#[#"gpsflighttrial#gcap.eu"]];
[mailer setSubject:self.subject.text];
[mailer setMessageBody:self.message.text isHTML:NO];
[mailer addAttachmentData:attachment mimeType:#"application/csv" fileName:#"results.csv"];
[self presentModalViewController:mailer animated:YES];
Just in case anyone has this problem in the future.
Related
It would seem that my apps no longer are able to save files to their documents folder when doing this:
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *dta = [[NSData alloc] init];
dta = [NSData dataWithContentsOfFile:path];
NSLog(#"writing file: %#", path);
if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(#"writeToFile error");
}else {
NSLog(#"Written: %#", path);
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
I do get "Written" in the log with the full path:
Written: /var/mobile/Containers/Data/Application/ECBCD65D-A990-4758-A07F-ECE48E269278/Documents/9d440408-4758-4219-9c9c-12fe69cf82f2_pdf.pdf
And as long as I don't quit the app I can load the PDF in a UIWebView like this (pdfPath is a string that I pass to the function that loads the PDF file):
[www loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfPath]]];
Any help at all is as always greatly appreciated.
Okay, after mucking about I found that since the documents directory changes path every time I run the app, I had to:
Write the file to the documents folder with the complete path to
the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: fileName];
NSData *dta = [[NSData alloc] init];
dta = [NSData dataWithContentsOfFile:path];
if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(#"writeToFile error");
}else {
NSLog(#"Written: %#", path);
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
Save only the file name to my database
Load the file by using the full newly generated Documents directory path and append the file name from my database:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: pdfPath];
I'm using the below code to send a csv via email. The email is send and I received in my Inbox, but I'm not receiving the csv file attached, any idea about what is wrong?
-(void)generateCSV
{
NSFileManager *filemgr = [NSFileManager defaultManager];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
if ([filemgr fileExistsAtPath:[NSString stringWithFormat:#"%#/test.csv", docsDir] ] == NO)
{
[filemgr createFileAtPath:[NSString stringWithFormat:#"%#/test.csv", docsDir] contents:[#"" dataUsingEncoding: NSUnicodeStringEncoding] attributes: nil];
}
surveyarray = [db retrieveSurvey];
CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:[NSString stringWithFormat:#"%#/test.csv", docsDir] atomic:NO];
NSString *str = [NSString stringWithFormat: #"No, CreatedDate, Question1, Question2, Question3, Comment\n"];
[csvWriter writeField:str];
for(int i = 0; i < surveyarray.count; i++)
{
survey = [surveyarray objectAtIndex:i];
NSString *str = [NSString stringWithFormat: #"%d, %#, %#, %#, %#, %#\n", i, survey.createddate, survey.question1, survey.question2, survey.question3, survey.comment];
[csvWriter writeField:str];
}
NSMutableString *subject = [[NSMutableString alloc] init];
NSMutableString *emailBody = [[NSMutableString alloc] init];
[subject appendString:#""];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:subject];
NSArray *toRecipients = [NSArray arrayWithObjects:#"", nil];
[mailer setToRecipients:toRecipients];
NSData *myData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:#"%#/test.csv", docsDir]];
NSLog(#"%#", myData);
[mailer addAttachmentData:myData mimeType:#"text/csv" fileName:#"test.csv"];
[mailer setMessageBody:emailBody isHTML:NO];
mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:mailer animated:YES];
[mailer release];
[subject release];
[emailBody release];
[csvWriter release];
}
I am generating csv file then send email as attachment.
I open the email in my mailbox, it does not content any attachment.
According to what I see there, I'd say that you forgot to add .csv at the end of your filename :)
According to the NSData class reference, the path given with dataWithContentsOfFile: should be the absolute path. So you should use
[NSString stringWithFormat:#"%#/test.csv", docsDir]
instead of just #"test.csv"
I bet currently your myData is nil or empty.
By the way, I would create an NSString object with the fully qualified path/filename only once and re-use that rather than creating it 3 or 4 times.
This code works for me every time!
http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865
try replacing above line this
[mailer addAttachmentData:myData mimeType:#"text/csv" fileName:#"test.csv"];
finally I solved the problem. The email did attached attachment just the file is empty. This is because the for loop initial with -1 and i change to int i = 0, it works fine now
I tried this with no luck:
NSData *imageData = UIImageJPEGRepresentation([sight.photo valueForKey:#"image"], 0.8);
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsPath stringByAppendingPathComponent:#"sound.m4a"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSData *aacData = [sight.sound valueForKey:#"soundrecord"];
[aacData writeToURL:fileUrl atomically:YES];
// NSString *text = self.textField.text;
NSArray *items = #[imageData, aacData];
Yes, but you need to pass the audio file URL to the activity sheet rather than the NSData.
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:#"sound.aac"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSArray *activityItems = #[fileUrl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
If you find that the attachment doesn't appear when launching the email composer from the activity sheet, make sure your file path and URL are correct and that the file exists (including the extension on the device/simulator drive).
To open a file on the simulator, go to folder (~/Library/Application Support/iPhone Simulator/...
To check the file name on device, go to XCode Organizer Window and select the Applications tab within the debugging device connected. You should see the file structure on the main panel.
Dropbox restClient saves only files. So i want to save the image in local folder first and then to upload it, as a result it saves file, but it is corrupted.
NSString *localPath = [[NSBundle mainBundle] pathForResource:#"Info" ofType:#"plist"];
NSString *jpegFilePath = [NSString stringWithFormat:#"%#/test.jpeg",localPath];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data2 writeToFile:jpegFilePath atomically:YES];
NSString *filename = #"test.jpeg";
NSString *destDir = #"/";
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:localPath];
I am an idiot, solved
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingString:#"test.jpg"];
NSData * data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data writeToFile:path atomically:YES];
[self.restClient uploadFile:#"test.jpg" toPath:#"/" withParentRev:nil fromPath:path];
you will have to call DBRestClient methods from the main thread or a thread that has a run loop. Otherwise the delegate methods will not be called.
what you will have to do is first alloc init yoir DBRestClient object then make it's delegate self and then you can easily upload your file.below is an example
NSString *destDir = #"/";
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
[restClient uploadFile:yourfilename toPath:destDir withParentRev:nil fromPath:sourcepath];
Happy Coding!!!!!!
You can't save into the bundle. You should use documents or cache folder:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
I am developing an iOS application, and trying to zip the file I have created in the application, is there any built in function able to do this?
I definitely recommend Objective-Zip. It just recently moved to https://github.com/flyingdolphinstudio/Objective-Zip:
Some examples from their documentation:
Creating a Zip File:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:#"test.zip" mode:ZipFileModeCreate];
Adding a file to a zip file:
ZipWriteStream *stream= [zipFile writeFileInZipWithName:#"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
Reading a file from a zip file:
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:#"test.zip" mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
As Alex pointed out, I responded to this question by pointing out the NSData category contributed by users of the Cocoadev wiki. That category includes methods for dealing with zipped and gzipped data in an NSData instance (that could be read from a Zip file or written to one). This should be all you need to implement the file zipping you describe, as long as you can feed your file data into an NSData instance.
For an example of this category in action, please see the source code for my iPhone application, Molecules. I only use the method to extract data from a gzipped file (in SLSMolecule+PDB.m), but you should be able to get the basic concepts from that.
first you download Objective-zip example from http://code.google.com/p/objective-zip/downloads/list
in this example Find and copy three folder Objective-Zip, MiniZip and ZLib drag in to your project
import two class in you .m class
"ZipFile.h" and
"ZipWriteStream.h"
create method of zip my code is :-
-(IBAction)Zip{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
NSString *ZipLibrary = [paths objectAtIndex:0];
NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:#"backUp.zip"];
//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];
//self.documentsDir = [paths objectAtIndex:0];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];
NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files){
for(int i = 0;i<files.count;i++){
id myArrayElement = [files objectAtIndex:i];
if([myArrayElement rangeOfString:#".png" ].location !=NSNotFound){
NSLog(#"add %#", myArrayElement);
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(#"%d",data);
[streem writeData:data];
[streem finishedWriting];
}else if([myArrayElement rangeOfString:#".txt" ].location !=NSNotFound)
{
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(#"%d",data);
[streem writeData:data];
[streem finishedWriting];
}
}
[self testcsv];
[zipFile close];
}
your documents directory saved item .png and .txt files zipping in Library folder with backup.zip
i hope this is helps