NSSavePanel Error

While trying to use the NSSavePanel, there is an error at run time.

The Error is:
ERROR: Unable to display save panel: your app has the User Selected File Read entitlement but it needs User Selected File Read/Write to display save panels. Please ensure that your app's target capabilities include the proper entitlements.

The code is:
        let targetPath = NSHomeDirectory()
        let mySave = NSSavePanel()
        mySave.title = "Save a File Here"
        mySave.prompt = "Save Me"
        mySave.directoryURL = URL(fileURLWithPath: targetPath.appending("/Desktop"))
        mySave.canCreateDirectories = true
        mySave.showsHiddenFiles = false
        mySave.allowedFileTypes = ["adi"]

Note: I did add code for using the NSSavePanel to ensure the program could write directly to a file. That code worked as expected. The the program can write to a file.
Your question is not clear. You say:

Note: I did add code for using the NSSavePanel to ensure the program could write directly to a file. That code worked as expected. The the program can write to a file.

Which code is this ?
Which is the code that doesn't work.

The message seems clear:
  • Open your app's entitlements

  • Do you have an entry in the plist:

com.apple.security.files.user-selected.read-write
  • Is it set to true (1) ?

The app's entitlements does NOT have "com.apple.security.files.user-selected.read-write". How do you add this?

The app's entitlement does have "com.apple.security.files.user-selected.read-only". This has been set to both 1 and 0 with the same error occurring.

Thanks for helping


The app's entitlements does NOT have "com.apple.security.files.user-selected.read-write". How do you add this?

Assuming your app is a sandboxed macOS app.

Open the project file and choose the Signing & Capabilities tab of the target.
You can find the User Selected File in File Access Type of App Sandbox.

Check the value is set to Read/Write. The entitlement is automatically managed by this setting.
Thanks for your help it worked.

The app's entitlements does NOT have "com.apple.security.files.user-selected.read-write"

Note that you could also add manually this entry in the entitlements plist (auto completion works there as well as you type com.apple.security.files.user).

So that was the root cause.
Don't forget to close the thread.
NSSavePanel Error
 
 
Q