Reading files outside of sandbox

In a sandboxed application for MAS users may drag (or select) picture files to the application, the paths are then saved into a database.
Later I need to open this files for reading which works if the application remains open but once closed and reopened I have no longer access to this files.

I would need to know the best (fastest) way to:
1.- Check if the application has access to a file
2.- If access is denied elevate permissions to open the files. Is a folder select dialog the recommended method regarding that other files are probably in the same folder?

Usually this files will be in the Pictures folder and I may grant this privilege using App Wrapper but also need a solution for files in different folders.

Thanks.

The best and probably only solution to access another folder is to use an open dialog. If the user does not have access, there is no way to elevate privileges I know of.

I haven’t worked with them, but I believe you need to use something called “Security Scoped Bookmarks”.

Which Sam seems to tell people to stay away from (unless I mis-understood his posts)

Which I do; they are a dirty hack for a fundamental problem. I refer to them as bodgemarks, because after using them in apps, you’ll see that they are a high assed effort at solving the problem.

If I were you, so would copy the image data into your database, not the path, why you ask?

  1. A SSB requires that you’re application request permission to acces the file every time it needs to; you must release access to the file, otherwise you can cause App sandbox corruption and even bring the entire machine down.

  2. Files linked via SSB are incompatible with a lot of Apple’s own APIs, including the image API. Why? Because Apple over time have chosen to use ‘Lazy Loading’ in more and more places, which means that the actual image data isn’t loaded there and then (unless you specifically specify this when using CGImageSource), it’s often loaded after you’ve released access. Images will store a cached version, if you change the display size of the image it needs to reload it again. To get around this we copy the images into our data files.

  3. When a SSB fails for whatever reason (the wind changed direction or you’re wearing the wrong colour underpants) that’s it you’re cut off, you can’t even find out the name or path of the file without storing it yourself first.

  4. I just discovered recently when using SSBs to link to movies, that for some reason movies don’t use Lazy Loading, but once you’ve loaded the movie the SSB will fail subsequently all the time the movie is open, even if you’ve released access (so keep a hold of that folderitem).

  5. Do not use SSBs to access files that contain SSBs, while it’s possible, it nearly always goes wrong somehow and kills the App container. Was told that technically it’s possible; but Apple never expected them to be used in this way so don’t do it!

Thanks for your help.

I’ll follow your advice, Sam, and not use SSB, thanks for the great explanation.

Copying the images into my database, or just copying the files into my data bundle (I’m using a folder as data file due to the issues with SQLite databases and journal files) is not the preferable solution because they may be even thousands of large image files; in the best case they will be already in the Pictures folder.

I believe following Michel’s answer will work and I only have to ask for the folder if the are not accessible.

But still wondering which is the fastest way to know if I have access, may be with a stream reading a few bytes?

Thanks

Folderitem.readable

Sam, thanks so much. With help of your great App Wrapper tool I’m getting it working in no time, didn’t knew I could use it also for debugging in sandbox.