【发布时间】:2015-05-07 16:26:19
【问题描述】:
我在 Core Data 中存储了一些数据。我成功地将核心数据记录导出为 CSV 文件,并将该 CSV 文件作为附件发送给最终用户。
我知道 iPhone 不允许保存图片以外的附件。相反,它提供了一些选项来查看 pdf 等附件。
但我想将通过邮件附加的 CSV 文件保存在 iCloud/alternative 中,并且我能够获取保存的 CSV 文件的路径。要再次将该记录保存到核心数据,即使用户在导出后删除了所有核心数据记录他们并单击导入选项。
到目前为止,我已经完成了 CSV 文件的导出和邮件的发送。我也完成了将 CSV 文件保存在文档目录中并解析 CSV 文件并再次保存到核心数据..
下面是我的代码..使用文档目录。
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];
}
}
}
任何关于如何保存 CSV 附件的想法将不胜感激。
提前致谢..
【问题讨论】:
-
我已经完成了所有的工作..邮寄 CSV 文件..如上面链接中所做的那样..我需要将附件保存在 iCloud 或任何其他文件管理器中...
标签: ios objective-c csv email-attachments