Re: Security scoped bookmarks example?
Re: Security scoped bookmarks example?
- Subject: Re: Security scoped bookmarks example?
- From: Quincey Morris via Cocoa-dev <email@hidden>
- Date: Wed, 4 Mar 2020 07:57:15 -0800
On Mar 4, 2020, at 06:12 , Gabriel Zachmann via Cocoa-dev
<email@hidden> wrote:
>
> // load new image from disk
> NSURL * url = [NSURL fileURLWithPath: [self absolutePathFor: filename_]
> isDirectory: NO];
> // filename_ is one of the paths from the list of images under the user
> selected directory
> // at this point, url is a proper URL, I have checked
>
> [url startAccessingSecurityScopedResource];
> CGImageSourceRef sourceRef = CGImageSourceCreateWithURL( (CFURLRef) url,
> NULL );
This isn’t even nearly correct. You *must* call
“startAccessingSecurityScopedResource” on a URL that was reconstituted from a
security scoped bookmark. According to this, you called it on a URL that you
arbitrarily created. Why would you even expect that to work? If it did, then
you could access any file without user permission, and security scoped URLs
would have no purpose.
> I create the SSB like this:
>
> directoryBookmark_ = [dir bookmarkDataWithOptions:
> NSURLBookmarkCreationWithSecurityScope |
> NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
> includingResourceValuesForKeys: nil
> relativeToURL: nil
> error: &systemError];
So, you created a security scoped bookmark, then what did you do with it? You
should store it in user defaults, then load it from user defaults the next time
your code starts up, reconstitute it back into a security scoped URL, and then
use *that* URL (literally that object, not a different instance based on the
same path) as the base URL for all the image file URLs you need.
Note that you don’t need the bookmark to be reconstituted while your code is
still running after using the open panel, because the URL that the open panel
returns is *already* a security scoped URL in the current process. You only
need to use the bookmark if your code starts up again in a new process.
Note also that you must not *re*-create the bookmark from a reconstituted URL.
That doesn’t work. You must create the security scoped bookmark *only* from the
fresh URL that the open panel gives you. Store it once, then keep
reconstituting it as needed.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden