Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac App Store sandboxing and handling security-scoped bookmarks prior to 10.7.3

I need my sandboxed app to reopen an opened file after the app is restarted. Apple provides security-scoped bookmarks with the NSURLBookmarkCreationWithSecurityScope and NSURLBookmarkResolutionWithSecurityScope options in the NSURL bookmark creation and resolving methods. However, these flags/options are only good for 10.7.3 or later and cause an app prior to 10.7.3 to fail.

How do I handle retention/reopening of the file bookmark for 10.6 to 10.7.3 in a sandboxed app?

--

FOLLOW-UP: Please see my answer below. The issue was not caused by using NSURLBookmarkCreationWithSecurityScope but by using the security-scoped bookmark start and stop methods.

like image 499
spurgeon Avatar asked Aug 30 '12 01:08

spurgeon


1 Answers

It turns out using NSURLBookmarkCreationWithSecurityScope does not cause an issue with 10.7 - 10.7.2. What causes the failure is calling -[NSURL startAccessingSecurityScopedResource]: which is not supported prior to 10.7.3. Therefore, you need to wrap calls to this method (and the corresponding stop method) with an OS check or a respondsToSelector check. I tested that the bookmark still works in 10.7.1 as long as you make sure not to call start/stop.

Here are some code snippet for using respondsToSelector that will help any others that run in to this problem:

Use this to start usage:

if([bookmarkFileURL respondsToSelector:@selector(startAccessingSecurityScopedResource)]) { // only supported by 10.7.3 or later
    [bookmarkFileURL startAccessingSecurityScopedResource]; // start using bookmarked resource
}

And this to stop usage:

if([bookmarkFileURL respondsToSelector:@selector(stopAccessingSecurityScopedResource)]) { // only supported by 10.7.3 or later
    [bookmarkFileURL stopAccessingSecurityScopedResource]; // stop using bookmarked resource
}
like image 195
spurgeon Avatar answered Nov 03 '22 09:11

spurgeon