Question

I'm sandboxing an app (which has been running successfully non-sandboxed for a few years).

It takes batched of PDFs and exports them with some changes.

Users have two options:

-export resulting PDFs into a specified folder, or

-export PDFs into the same folder as original file.

There's no problem with the first option: users select a folder in NSOpenPanel, I create security-scoped bookmark, resolve it later, and the files are being successfully saved.

However, when user chooses to save export each file into its enclosing folder, they don't select any folder explicitly. They only select files.

I can create security-scoped bookmark for each file, but it still doesn't grant me access to file's enclosing folder. I get permission errors when I try to save files.

When I try to create a bookmark to the enclosing folder, I get an error which says that the file can't be open:

NSURL *url = [NSURL fileURLWithPath:[filePath stringByDeletingLastPathComponent]];

NSData *bookmarkData = [url
                       bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                       includingResourceValuesForKeys:nil
                       relativeToURL:nil
                       error:&err];

As far as I understand, it happens because user doesn't explicitly select the folder and therefore secured-scoped bookmark cannot be created.

I did extensive research and tried various option but couldn't get anywhere.

Any help would be appreciated!

Was it helpful?

Solution

Your understanding, as expressed at the end of your question, is correct. You need to ask the user for permission to the enclosing folder or come up with another design. There is an Apple solution if you are changing the file extension (e.g. so editors can convert file formats etc.), but not for your case where it is PDF in, PDF out. Cache bookmarks to the enclosing folders and over time your app will need to ask the user less often (remember a bookmark to a folder grants access to the whole sub tree below that point).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top