保存文件时权限错误(沙箱)

Seb*_*hec 4 macos cocoa save appstore-sandbox

我正在尝试将文件保存到路径中,在沙盒应用程序[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"}
Run Code Online (Sandbox Code Playgroud)

我已将权限的"用户所选文件"设置为"读/写访问权限".

我的代码..

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];
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我该如何保存文件?

谢谢.

usa*_*ain 10

要在沙箱外读/写文件,您必须获得用户对该文件或上述目录之一的访问权限.访问可以通过使用来获得NSOpenPanel,NSSavePanel或拖放.

应用程序终止后,对这些文件/目录的访问将丢失.

为了永久访问用户选择的文件/目录,您必须使用Security-Scoped Bookmarks.

  • 我已经完成了请求权限并在此课程中保留该权限的过程,如果您愿意,可以使用它.https://github.com/leighmcculloch/AppSandboxFileAccess (2认同)