所以我正在创建一个 .csv 文件,然后允许用户使用 UIActivityViewController 共享它。
我创建 csv 文件的代码将返回文件的 NSURL:
- (NSURL *)exportToCSV
{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:@"results.csv"];
if (![[NSFileManager defaultManager] fileExistsAtPath:docPath]) {
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil
attributes:nil];
}
NSMutableString *contents = [NSMutableString stringWithCapacity:0];
//fill contents with data in csv format
// ...
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fileHandle writeData:[contents dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
return [NSURL fileURLWithPath:filePath];
}
然后我的活动使用该 NSURL 来启动 UIActivityViewController:
- (IBAction)shareButtonPressed:(id)sender {
NSArray *activityItems = @[@"results.csv", [self.object exportToCSV]];
UIActivityViewController *shareScreen = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:shareScreen animated:YES completion:nil];
}
当我选择邮件选项时,未附加 csv 文件。它只有文本“results.csv”
我究竟做错了什么?