NSSavePanel does not save file after application sandbox

I had a problem saving a string file with NSSavePanel after the sandbox of the Mac App Store app. I set com.apple.security.files.user-selected.read-write to YES and NSOpenPanel works as it should.

When I try to save a new file, it seems that everything is working fine, but then there is no saved file where it should be ....

This is the code I use to save the file:

 NSSavePanel *save = [NSSavePanel savePanel]; long int result = [save runModal]; if (result == NSOKButton) { NSString *selectedFile = [save filename]; NSString *fileName = [[NSString alloc] initWithFormat:@"%@.dat", selectedFile]; NSString *arrayCompleto = [[NSString alloc]initWithFormat:@"bla bla bla"]; [arrayCompleto writeToFile:fileName atomically:NO encoding:NSUTF8StringEncoding error:nil]; } 
+8
objective-c cocoa appstore-sandbox nssavepanel
source share
1 answer

First of all, the selector -[NSSavePanel filename] deprecated. Use -[NSSavePanel URL] . Secondly, the way that -[NSString writeToFile:atomically:encoding:error] tells you what you are doing wrong is with the error:(NSError**) argument.

You should also handle errors for file I / O, in particular, because even if your code is 100% right, there may still be errors in the user system (insufficient rights, etc.) and the presentation of the error to the user allows them to see that this did not work (and have an idea why). Handling an error in the code will also allow your application to recover. For example, if you tried to read in a file under the code that you inserted (after writing to the disk), but the user tried to write it to a network share that they did not have access to, your application may crash. If you know that the recording failed, you can act accordingly (perhaps by requesting a different save location).

In this case, I believe the following line is your problem:

 NSString *fileName = [[NSString alloc] initWithFormat:@"%@.dat", selectedFile]; 

When your application is sandboxed, the user must grant you permission for a specific file or directory through open / saved panels in order to bring them into their sandbox. What you do is take the file that the user gave you permission to write, and say "it's great, but I want to save another file", which violates the sandbox. Instead, you should install the extension in the Save panel. Complete fixed solution:

 NSSavePanel *save = [NSSavePanel savePanel]; [save setAllowedFileTypes:[NSArray arrayWithObject:@"dat"]]; [save setAllowsOtherFileTypes:NO]; NSInteger result = [save runModal]; if (result == NSOKButton) { NSString *selectedFile = [[save URL] path]; NSString *arrayCompleto = @"bla bla bla"; NSError *error = nil; [arrayCompleto writeToFile:selectedFile atomically:NO encoding:NSUTF8StringEncoding error:&error]; } if (error) { // This is one way to handle the error, as an example [NSApp presentError:error]; } 

If something else is wrong in the future, you can check the error value at runtime. During debugging, set a breakpoint inside the if (error) to check the value of the error object (make po error in the Xcode debugger). This should help you understand what is wrong.

+10
source share

All Articles