我正在尝试将NSString
写入文件,然后在iTunes中查看该文件。我将Application supports iTunes file sharing
密钥设置为YES
,我可以在iTunes中看到文件共享。但是文件永远不会出现。
以下是我编写文件的方式:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSString *fileName = [dateFormatter stringFromDate:[NSDate date]];
fileName = [fileName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
fileName = [fileName stringByReplacingOccurrencesOfString:@"," withString:@""];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString* rootPath = paths[0];
NSString *path = [rootPath stringByAppendingPathComponent:fileName];
path = [path stringByAppendingPathExtension:@"txt"];
NSURL *url = [NSURL fileURLWithPath:path];
NSString *writeString = @"Hello world";
NSError *error;
BOOL ok = [writeString writeToURL:url atomically:YES encoding:NSUnicodeStringEncoding error:&error];
if (!ok) {
NSLog(@"%@", error);
}
else {
NSLog(@"File saved to: '%@'", path);
}
这是文件位置:
'/var/mobile/Applications/066FD6CD-0E5F-4701-9344-46258ABF0E97/Library/Wednesday_September_17_2014.txt'
并且好的iTunes:
答案 0 :(得分:1)
您希望NSDocumentDirectory
不是NSLibraryDirectory