gpt4 book ai didi

objective-c - 使用 App Group UserDefaults 在 Finder 同步扩展中使用安全范围书签

转载 作者:太空狗 更新时间:2023-10-30 03:38:39 25 4
gpt4 key购买 nike

在我的 finder 同步扩展中解析安全范围书签时出现以下错误。

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

还有可能是 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

我正在使用以下代码在容器应用程序中创建安全范围书签:

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

在 Finder 同步扩展中,我使用以下代码:

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

我还添加了这个授权 key :Finder 同步扩展和容器应用程序中的 com.apple.security.files.bookmarks.app-scope

我是 cocoa 编程的新手,无法找到问题所在。

我做错了什么?有人可以帮我吗?

最佳答案

要保存只读书签数据,请使用位掩码[.withSecurityScope, .securityScopeAllowOnlyReadAccess]不仅仅是.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)

仅使用 .securityScopeAllowOnlyReadAccess 在从主应用程序或扩展程序解析时给我问题中提到的错误:

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

关于objective-c - 使用 App Group UserDefaults 在 Finder 同步扩展中使用安全范围书签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com