Search code examples
objective-cxcodecocoasandboxfile-access

Sanboxed app, include files by reference?


So I have a sandboxed app, which allows users to drag and drop files from Finder onto a project window. I would like to use these files from their existing location, ie by reference, rather than copy them to a project folder, as the user may have a large existing library of files.

While the app is running, these files are fully available for reading. (They are .mp4 and I can preview them in an AVPlayer view).

When I save the app's project, I include the NSURL of the files. When the app restarts and re-opens the project later, the files are not available and Console shows deny(1) file-read-data - which means the file is not reaable. (Access denied).

Presumably this is because the files are out-side of the sandbox. Yet, they were readable when dropped onto the app. Looks to me like the app sees them as "user selected" when first dropped on, and so they are readable, but on the next session because they are loaded from NSCoder as NSURLs they are not considered user-selected and therefore become unaccessible!

My app currently has these entitlements: com.apple.security.files.user-selected.read-only com.apple.security.files.user-selected.read-write

Is there soemthing I need to do with entitlements etc to allow the re-loading of files later?


Solution

  • When the file is first dropped you need to create and save a security scoped bookmark which you can the use on a subsequent app launch to regain access to the file. Read Security-Scoped Bookmarks and Persistent Resource Access in Apple's App Sandbox in Depth.