[NSSavePanel savePanel] returns nil?

I'm working on a Unity app that includes a native plugin to save images. Debug builds, so far, work fine – as does the code-signed build to test in-app purchases, but once I include sandbox entitlements, we're unable to save screenshots. This isn't entirely unexpected, as I haven't done much with app sandboxing before...


But when I attach the app to Xcode to step through the native plugin code, to see where it's breaking down, I see that where it's failing is in creating the NSSavePanel itself. [NSSavePanel savePanel] simply returns nil. No errors, no exceptions, nothing.


What exactly is going wrong? If it was an entitlements issue, I'd expect some error to be thrown. Since the part that handles the open/save panels is a framework, it itself doesn't have a Capabilities tab & I have to sign & add the entitlements from the command line.

Answered by TheCD in 318277022
If it was an entitlements issue, I'd expect some error to be thrown.

Indeed. I just tested using NSSavePanel without the proper entitlement and it throws a runtime exception.


But just to be sure, are you including the

com.apple.security.files.user-selected.read-write
key in your entitlements file? A basic entitlements file would look like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.files.user-selected.read-write</key>
  <true/>
</dict>
</plist>
Accepted Answer
If it was an entitlements issue, I'd expect some error to be thrown.

Indeed. I just tested using NSSavePanel without the proper entitlement and it throws a runtime exception.


But just to be sure, are you including the

com.apple.security.files.user-selected.read-write
key in your entitlements file? A basic entitlements file would look like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.files.user-selected.read-write</key>
  <true/>
</dict>
</plist>

The "guides" I found didn't mention that key, so no. Adding that did fix the issue.


However, I have no idea why it would just return nil.

Great! I'm also surprised it just returns nil.


And yes, the documentation is a mess again, as always. Here's the link in case you need it: https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW6

[NSSavePanel savePanel] returns nil?
 
 
Q