保存文件(沙盒)时出现权限错误 [英] Permissions error when saving file (sandbox)

查看:600
本文介绍了保存文件(沙盒)时出现权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在沙盒应用程序[OS X]中将文件保存到路径,但到目前为止,几乎每次尝试保存时都会收到错误。错误是..

I'm trying to save a file to a path, in a sandboxed application [OS X], but so far am getting an error almost every time I try to save. The error is..

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

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

I have set "User Selected File" of my entitlements to "Read/Write Access".

代码..

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

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

What am I doing wrong? How can I save the file?

感谢。

推荐答案

为了在沙盒外部读/写文件,必须让用户访问该文件或上述目录之一。可以使用 NSOpenPanel NSSavePanel 或拖放来获取访问权限。

In order to the read/write a file outside the sandbox, you must get user access to that file or one of the directory above. The access can be gained by using NSOpenPanel, NSSavePanel or drag and drop.

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

The access to those files/directories is lost after the app terminates.

为了永久访问用户选择的文件/目录,您必须使用安全范围的书签

In order to get permanent access to a file/directory selected by the user, you must use Security-Scoped Bookmarks.

这篇关于保存文件(沙盒)时出现权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆