所以我创建一个.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”
我做错了什么?
答案 0 :(得分:6)
问题似乎出现在fileExistsAtPath
行中。您似乎在说“如果文档目录不存在,则创建文件”。这当然不对。
就个人而言,我会失去那些东西并将其改为
- (NSURL *)exportToCSV
{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:@"results.csv"];
NSMutableString *contents = [NSMutableString stringWithCapacity:0];
//fill contents with data in csv format
// ...
NSError *error;
[contents writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
// check for the error
return [NSURL fileURLWithPath:filePath];
}
答案 1 :(得分:4)
我想分享我的UIActivityViewController解决方案并将文本共享为CSV文件。此解决方案适用于通过Mail共享,甚至可以保存到Dropbox。
@IBAction func shareCsv(sender: AnyObject) {
//Your CSV text
let str = self.descriptionText.text!
filename = getDocumentsDirectory().stringByAppendingPathComponent("file.csv")
do {
try str.writeToFile(filename!, atomically: true, encoding: NSUTF8StringEncoding)
let fileData = NSURL(fileURLWithPath: filename!)
let objectsToShare = [fileData]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
} catch {
print("cannot write file")
// failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
}
}
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
希望它有所帮助! :)
答案 2 :(得分:4)
这对我来说是一个Swift 3版本,在Mail中作为附件工作并保存在Dropbox中。
var csvDetailsString = ""
for myObject in myObjectsArray {
csvDetailsString = csvDetailsString.appending(myObject.someText)
csvDetailsString = csvDetailsString.appending(",")
csvDetailsString = csvDetailsString.appending("More Stuff")
csvDetailsString = csvDetailsString.appending("\n")
}
let fileName = "Output"
let docDirectory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
if let fileURL = docDirectory?.appendingPathComponent(fileName).appendingPathExtension("csv") {
do {
try csvDetailsString.write(to: fileURL, atomically: true, encoding: .utf8)
let objectsToShare = [fileURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
}