启用应用组时使用“默认” CLI:“域不存在”

问题描述

我正在开发一个带有嵌入式Finder Extension的macOS应用,并且我希望该应用和扩展程序共享UserDefaults

该应用已被沙盒化,以便将来在应用商店中分发。

在这里遵循了指示:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

  1. 两个目标都是同一个应用程序组的一部分,如权利所述:

     <key>com.apple.security.app-sandBox</key>
     <true/>
     <key>com.apple.security.application-groups</key>
     <array>
         <string>$(TeamIdentifierPrefix)com.stormacq.mac.MyApp</string>
     </array>
     <key>com.apple.security.files.user-selected.read-only</key>
     <true/>
    
  2. 我正在通过suiteName访问UserDefaults:

    let defaults = UserDefaults.init(suiteName: "TEAM_ID.com.stormacq.mac.MyApp")
    
    //where TEAM_ID is the actual team id used to sign the app (something like 123AB45CDE)
    

但是,当尝试在App启动时读取或写入认值时,我收到此错误消息:

[用户认设置]无法读取 CFPrefsPlistSource (域: TEAM_ID.com.stormacq.mac.MyApp,用户kcfPreferencesAnyUser,ByHost: 是,容器:(空),需要刷新的内容:是):使用 带有容器的kcfPreferencesAnyUser仅适用于系统 与cfprefsd分离的容器

我发现错误消息显示Container: (null),很奇怪。

将创建组容器和共享的首选项文件(并包含我的代码设置的正确认值):

➜  ~ ls -al ~/Library/Group\ Containers/TEAM_ID.com.stormacq.mac.MyApp/Library/Preferences/                                             
total 8
drwx------  3 stormacq  1896053708   96 Sep  6 19:58 .
drwx------  5 stormacq  1896053708  160 Sep  6 19:58 ..
-rw-------@ 1 stormacq  1896053708  103 Sep  6 19:58 TEAM_ID.com.stormacq.mac.MyApp.plist

# I edited TEAM_ID to post this message

我正在使用Xcode 11.7(Swift 5)和macOS 10.15.6。

已纠正在~/Library/Group Containers目录中创建的共享首选项文件,但defaults命令行看不到它。

➜  ~ defaults read com.stormacq.mac.MyApp
2020-09-07 10:25:53.188 defaults[82827:947786] 
Domain com.stormacq.mac.MyApp does not exist

➜  ~ defaults read TEAM_ID.com.stormacq.mac.MyApp
2020-09-07 10:26:05.088 defaults[82857:947925] 
Domain TEAM_ID.com.stormacq.mac.MyApp does not exist

与非共享首选项相同的功能正常工作。

defaults可以读Group Containers吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)