问题
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?
回答1:
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];
回答2:
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."
回答3:
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:
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.
If that won’t work, then the other option is to create a document scoped, security scoped bookmark instead.
来源:https://stackoverflow.com/questions/37897118/using-security-scoped-bookmark-in-finder-sync-extension-with-app-group-userdefau