问题
I have the following save panel in my app. I have recently tried to sandbox it but unfortunately when sandboxed saving doesn't seem to be working.
I understand that beginSheetForDirectory is depreciated, so possibly that's why it's not working? How can I get this to work with sandboxing?
- (IBAction)saveButtonPressed:(id)sender
{
NSSavePanel *sp = [NSSavePanel savePanel];
[sp setRequiredFileType:@"jpg"];
[sp beginSheetForDirectory:[NSString stringWithFormat:@"%@/Pictures", NSHomeDirectory()]
file:@"output.jpg"
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(didEndSaveSheet:returnCode:conextInfo:)
contextInfo:nil];
}
-(void)didEndSaveSheet:(NSSavePanel *)savePanel
returnCode:(int)returnCode conextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton)
{
self.baseFilename = [[savePanel filename] stringByDeletingPathExtension];
tileHeight = [heightTextField intValue];
tileWidth = [widthTextField intValue];
[self performSelector:@selector(delayPresentSheet) withObject:nil afterDelay:0.1];
}
}
回答1:
When you are sandboxed, you cannot just access any file. Your app needs permission to access that file. So any API that gives you a path cannot possibly work. It would be impossible for the sandbox to figure out whether the filename came from a save panel or not.
There are new APIs returning a URL, and these URLs somehow have the permission for accessing a file built into them. You need to use that URL; if you want to use it later (after you quit and restart the app) you have to store the URL together with the mission.
That's just the principles how it works, you need to consult Apple's documentation for details. The problem isn't that the old API is deprecated, the problem is that it doesn't give you a special URL that includes permissions for the sandbox.
来源:https://stackoverflow.com/questions/30061192/nssavepanel-not-working-when-sandboxed-osx-objective-c