macos - 保存文件时出现权限错误(沙盒)

标签 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"}

我已将我的权限中的“用户选择的文件”设置为“读/写访问”。

我的代码..

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];
}

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

谢谢。

最佳答案

为了在沙箱之外读取/写入文件,您必须获得该文件或上述目录之一的用户访问权限。可以通过使用 NSOpenPanelNSSavePanel 或拖放来获得访问权限。

应用程序终止后,对这些文件/目录的访问权限就会丢失。

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

关于macos - 保存文件时出现权限错误(沙盒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139587/

相关文章:

MATLAB - 警告 : Variable 'vol' cannot be saved to a MAT-file whose version is older than 7. 3?

objective-c - 获取 OSX 连接的 Wi-Fi 网络名称

objective-c - XML 文件无法通过基于文档的 OSX 应用程序打开

objective-c - 包含文件时出错

java - 将二维数组保存到磁盘文件

javascript - 在内存中生成下载文件供用户下载,文件名用户设置

macos - 如何在 OS X 10.4 上的 AppleScript do shell 脚本中使用 UTF-8 编码命令

ruby - chruby 和 chgems 能代替 rvm 的 gemset 吗?

node.js - npm 不支持 Node.js v0.10.32

cocoa - 使用 8 条三次贝塞尔曲线创建(近似)圆的控制点是哪些