I am having some data storing in Core Data. I successfully export the core data records into CSV file and send that CSV file to end user as attachment.
I am to know iPhone does not allow to save the attachments other than image. Rather it gives some options to view the attachment like pdf.
But I want to save the CSV file that is attached through mail in iCloud/alternative and I able to get the path of CSV file saved.To save that records to core data again even user deleted all the core data records after exporting them & clicked import option.
Till now I have done exporting the CSV file and sending mail. Also I have done saving the CSV file in documents directory and parsing the CSV file and saving to core data again..
Below is my code..using Documents Directory.
AppDelegate *appDelegate;
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@“sample.csv"];
if (filePath) {
NSString * myText = [[NSString alloc]
initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];
if (myText) {
__block int count = 0;
[myText enumerateLinesUsingBlock:^(NSString * line, BOOL * stop) {
line=[line stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
NSArray *lineComponents=[line componentsSeparatedByString:@" "];
if(lineComponents){
float f=[[lineComponents objectAtIndex:0] floatValue];
NSNumber *number=[NSNumber numberWithFloat:f];
NSString *string1=[lineComponents objectAtIndex:1];
NSString *string2=[lineComponents objectAtIndex:2];
NSManagedObject *object=[NSEntityDescription insertNewObjectForEntityForName:@“Record” inManagedObjectContext:context];
[object setValue:number forKey:@"number"];
[object setValue:string1 forKey:@"string1"];
[object setValue:string2 forKey:@"string2"];
NSError *error;
count++;
if(count>=1000){
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
count=0;
}
}
}];
NSLog(@"done importing");
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
else
{
[[[UIAlertView alloc] initWithTitle:@"CSV" message:@"File Imported successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
}
}
Any ideas how to save the CSV attachment..will be appreciated.
Thanks in Advance..