Using Security Scoped Bookmark in Finder Sync Extension with App Group UserDefaults
Asked Answered
P

4

13

I am getting following error while resolving Security Scoped Bookmark in my finder sync extension.

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

and also possibly the related logging:

Failed to read values in CFPrefsPlistSource<0x6080000ee380> (Domain: MyAppGroupName, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null)): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd

I am using following code to create Security Scoped bookmark in Container App:

NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.mycomp.xyz"];
NSURL * theSelectedFolder = ....selected folder from NSOpenPanel....
NSData *bookmarkData = [theSelectedFolder bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];

[sharedDefaults setObject:bookmarkData forKey:@"BookmarkData"];
[sharedDefaults synchronize];

In Finder Sync Extension, I am using following code:

NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.mycomp.xyz"];
NSData *bookmarkData = [sharedDefaults objectForKey:@"BookmarkData"];
BOOL bookmarkDataIsStale;
NSError *err;
NSURL *userSelectedUrl = [NSURL URLByResolvingBookmarkData:bookmarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&bookmarkDataIsStale error:&err];

and i have also added this entitlement key: com.apple.security.files.bookmarks.app-scope in both Finder Sync Extension as well as Container app.

I am new to cocoa programming and not being able to find any luck finder the problem.

What am I doing wrong? Can anyone help me out here?

Porscheporsena answered 18/6, 2016 at 12:45 Comment(3)
"Share security scoped bookmark in app group?" forums.developer.apple.com/thread/66259Brasilin
"Bookmarked URLs to security scoped resources cannot be resolved in app extensions" openradar.appspot.com/43055392Brasilin
NSCocoaErrorDomain Code 259 is NSFileReadCorruptFileError, originating as kCFURLReadCorruptResourceErrorBrasilin
B
4

To save read-only bookmark data, use the bitmask [.withSecurityScope, .securityScopeAllowOnlyReadAccess]; not solely .securityScopeAllowOnlyReadAccess.

.securityScopeAllowOnlyReadAccess

When combined with the .withSecurityScope option, specifies that you want to create a security-scoped bookmark that, when resolved, provides a security-scoped URL allowing read-only access to a file-system resource; for use in an app that adopts App Sandbox.

https://developer.apple.com/documentation/corefoundation/cfurlbookmarkcreationoptions/1543362-securityscopeallowonlyreadaccess

url.bookmarkData(options: [.withSecurityScope, .securityScopeAllowOnlyReadAccess], includingResourceValuesForKeys: nil, relativeTo: nil)

Using only .securityScopeAllowOnlyReadAccess was giving me the error mentioned in the question when resolving from either the Main App or the Extension:

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

Brasilin answered 16/10, 2018 at 3:17 Comment(1)
This saved my life. Everything worked on macOS 12 (Monterey) but failed with error 259 on macOS 10 (Catalina) but now it works on both OS versions.Mchugh
B
2

I received a response from Apple Developer Technical support. Posted below:

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 to the other component, which can then create and save its 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.

Brasilin answered 19/10, 2018 at 0:40 Comment(2)
#2 didn't work (using document scoped security bookmark). I tried having the user select a file instead of a directory, but attempting to load the bookmark from the extension got the same error Error Domain=NSCocoaErrorDomain Code=259 "The file couldn’t be opened because it isn’t in the correct format."Stoichiometry
#1 worked! I found the exact steps also outlined here: developer.apple.com/forums/thread/…Stoichiometry
Z
0

The Problem was you're trying to resolve bookmarks data in finder Extension.You need resolved bookmarks data in containing app itself .

You can get selected item urls and targeted url in extension.

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.mycomp.xyz"];
NSURL* target = [[FIFinderSyncController defaultController] targetedURL];
NSArray* items = [[FIFinderSyncController defaultController] selectedItemURLs];
Zoellick answered 15/10, 2018 at 7:25 Comment(1)
Why can bookmark data not be resolved in an Extension? Do you have any sources for that claim?Brasilin
F
0

You can use the bookmark from the extension by using:

url.bookmarkData(options: [.minimalBookmark], includingResourceValuesForKeys: nil, relativeTo: nil)

and saving that bookmark to the App Group user defaults.

Finecut answered 13/7, 2021 at 15:19 Comment(1)
Please provide example code rather then just sharing suggestion and also your answer should be descriptive.Woundwort

© 2022 - 2024 — McMap. All rights reserved.