问题
I've been trying to write a Mac app lately, and have run into an issue. This is not overly major but it would be great to have a solution.
I have to load an image at runtime, specified by the user (so no, I cannot embed the image into the bundle as many alternatives suggest), and embed it into an NSTextView using NSAttributedText. The image loads fine from the Pictures directory (/Users/USERNAME/Pictures/IMAGE) etc, but not from the Desktop. I have checked the Capabilities tab in Xcode, and Read/Write is enabled for all of the available options:image
Using FileManager, I have determined that the file definitely exists, but is not readable. Can I set this manually in the Entitlements file?
Thanks in advance.
回答1:
No, you can't. The standard desktop is not accessible out of the box if the app is sandboxed. Only the standard Downloads
, Pictures
, Music
and Movies
folder are accessible with the corresponding entitlement.
There are two options to access the other folders but in all cases the user must choose them once
- Open/Save dialog for temporary access if the appropriate entitlement is set.
- A security scoped bookmark for permanent access but also in this case the user must first choose the folder once via Open/Save dialog and the security scoped bookmark must be created programmatically.
来源:https://stackoverflow.com/questions/51310678/desktop-sandboxing-issue-with-swift-macos