我正在尝试将文件保存到沙盒应用程序 [OS X] 中的路径,但到目前为止,几乎每次尝试保存时都会出现错误。错误是..
Error saving: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “test.txt” in the folder “Testing”." UserInfo=0x1001f5e70 {NSFilePath=/Users/Seb/Desktop/Testing/test.txt, NSUnderlyingError=0x1001f5d70 "The operation couldn’t be completed. Operation not permitted"}
我已将我的权限中的“用户选择的文件”设置为“读/写访问”。
我的代码..
NSString *saveLoc = [NSString stringWithFormat:@"%@/%@.txt",[[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] valueForKey:@"saveURL"]] path],self.theWindow.title];
NSURL *saveURL = [NSURL fileURLWithPath:saveLoc];
NSLog(@"Saving to: %@",saveLoc);
NSError *err = nil;
[self.textView.string writeToURL:saveURL atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (err) {
NSLog(@"Error saving: %@",err);
[[NSAlert alertWithError:err] beginSheetModalForWindow:self.theWindow
modalDelegate:nil
didEndSelector:NULL
contextInfo:nil];
}
我做错了什么?如何保存文件?
谢谢。
最佳答案
为了在沙箱之外读取/写入文件,您必须获得该文件或上述目录之一的用户访问权限。可以通过使用 NSOpenPanel
、NSSavePanel
或拖放来获得访问权限。
应用程序终止后,对这些文件/目录的访问权限就会丢失。
为了获得对用户选择的文件/目录的永久访问权限,您必须使用 Security-Scoped Bookmarks .
关于macos - 保存文件时出现权限错误(沙盒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139587/