-1

So for a while I have been build a survey for people to complete on their i-phones all the data is saved to a csv file then sent in a email via the app once complete... however im having trouble attaching the csv file! Let me explain.

My code was as follows:

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:[NSData dataWithContentsOfFile:@"results.csv"]
                 mimeType:@"application/csv"
                 fileName:@"results.csv"];
[self presentModalViewController:mailer animated:YES];

This would attach the csv file within the app and send it however upon receiving it the csv file would have disappeared (As if by magic!) to check the file was correct etc and saving the data it add the actual log so it looked like this:

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:[NSData dataWithContentsOfFile:@"/var/mobile/Applications/CD2A1913-DEE0-41DD-9F8A-00DD6793F565/Documents/results.csv"]
                 mimeType:@"application/csv"
                 fileName:@"results.csv"];
[self presentModalViewController:mailer animated:YES];

SUCCESS! I thought as the email was sent within the app and the attachment was there upon receiving it! However when trying it on another phone bam back to the same thing attachment is there but upon receiving it there was no atahcment to be seen. I checked the log again only to find this time the other phone was saving it /var/mobile/Applications/B866A8EC-3A50-4642-B09B-F7DB15C66433/Documents/results.csv hence why it wasnt finding the attachment!

My question is how can i get around this? Is there a generic path name for the i-phone? How can i get the app to find the file on everyones phone when its saved in a different folder?

Thank you for your time :)

June
  • 39
  • 7
  • I think you may want to take a look at this question: http://stackoverflow.com/questions/6907381/what-is-the-documents-directory-nsdocumentdirectory – Gregory Furmanek Nov 14 '14 at 19:13
  • Thank you for this sorry im new to iOS development and this has confused me more, what is the most simple way to find the file and attach it? I thought all iphones used the same path... – June Nov 14 '14 at 19:31

1 Answers1

0

There are directories you can store and access user files:

https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

You can get the path for Documents directory using this method from the article linked in my comment:

- (NSString *) applicationDocumentsDirectory {    
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  return basePath;
}


NSString *path = [self applicationDocumentDirectory];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"results.csv"];
NSData *data = [NSData dataWithContentsOfFile:filePath];

This is the location I would save the csv file and later retrieve it for sending.

The code itself has not been ran so I don't guarantee the syntax is correct. It is how I would go about solving this problem.

If you don't need persistence you could just store your data memory using NSData.

Gregory Furmanek
  • 364
  • 1
  • 13