Share security scoped bookmark in app group?

1) Main app and extension register for app groups.


2) Main app gets a url from NSOpenPanel, makes a security scoped bookmark and saves is in NSUserdefaults with group container as the suite name.


3) Run the extension, grab the bookmark data from user defaults and resolve it. The data is not nil, but fails to resolve:


Error Domain=NSCocoaErrorDomain Code=259 "The file couldn’t be opened because it isn’t in the correct format."

Accepted Reply

My bug report was closed as not a bug. Figured I'd let everyone know so they don't get 😠

Replies

This isn't even working, now, only from my Main App.


I set the main app to be Read-Only file access. Using the code below to create then immediately resolve the URL's bookmark data.


Using `.securityScopeAllowOnlyReadAccess` and then `.withSecurityScope`



        var stale = false
        guard let bookmarkData = try? url.bookmarkData(options: .securityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeTo: nil),
            let url = try? URL(resolvingBookmarkData: bookmarkData, options: [.withoutUI, .withSecurityScope], relativeTo: nil, bookmarkDataIsStale: &stale) else {
                print("error NSCocoaErrorDomain Code=259 :(")
            return
        }

I’m not sure what’s going on here and, alas, I don’t have time to dig into it in this context. Still, I’m hopeful that there might be a reasonable way to achieve this goal, so I’m going to recommend that you open a DTS tech support incident and talk to our bookmarks expert about this.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you, I have opened DTS `Follow-up: 701518883` for this issue.

One fix that I've found: URL `bookmarkData(options:` needs to use the bitmask `[.withSecurityScope, .securityScopeAllowOnlyReadAccess]` for read-only access. I had been using *only* `.securityScopeAllowOnlyReadAccess` when attempting to save read-only bookmarks. The combined bitmask requirement is listed in the docs, but not where the optionset is declared.


URLs saved like this refuse to Resolve and result in the Error Code 259. This is the issue I was having where the Main App could not resolve its own bookmarks. This is now working for both read and read-write bookmarks in the main app. The App Extension can still not resolve Data into URLs.


The App Group User Defaults is working fine as storage and I am using the exact same code path in both App and Extension. Only the main app can resolve the data.

Reply from DTS:


Follow-up: 701518883


I dug into this some more, so let me restart the conversation here:


> I need to share Sandbox Security Bookmarks between my main Mac App and its

> FinderSync extension.

>


The underlying issue here is that an app scoped, security scoped bookmark is

ONLY valid within the app that created it. That leaves you with two options:


1) If both app components are running at the same time, then you can send the

current URL the other component, which can then create and save it’s own app

scoped, security scoped bookmark.


2) If that won’t work, then the other option is to create a document scoped,

security scoped bookmark instead.