Permission error while saving file (sandbox)

I am trying to save a file in a path in a sandboxed application [OS X], but so far I get an error almost every time I try to save. Error...

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 set the "User selected file" of my rights 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 to save a file?

Thanks.

+4
source share
1 answer

To read / write a file outside the sandbox, you must access this file or one of the above directories. Access can be obtained using NSOpenPanel , NSSavePanel or drag and drop.

Access to these files / directories is lost after the application terminates.

To get permanent access to the file / directory selected by the user, you should use Bookmarks with security protection .

+10
source

Source: https://habr.com/ru/post/1442980/


All Articles