Permissions error when saving file (sandbox)

守給你的承諾、 提交于 2019-12-01 04:20:09

问题


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

My code..

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?

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/13139587/permissions-error-when-saving-file-sandbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!