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

眉间皱痕 提交于 2019-12-05 05:24:23
iosdev

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];

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."

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!