I am developing a macOS app, with an embedded Finder Extension and I want the app and the extension to share UserDefaults
The App is Sandboxed for future distribution on the app store.
I followed instructions here : https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1
Both targets are part of the same app group, as it can be seen in the Entitlements:
<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/>I am accessing the UserDefaults by the
suiteName: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)
However, when trying to read or write the defaults at App startup time, I receive this error message :
[User Defaults] Couldn't read values in CFPrefsPlistSource<0x600002c4d200> (Domain: TEAM_ID.com.stormacq.mac.MyApp, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd
I found weird that the error message says Container: (null),.
The group container and the shared Preference file is created (and contains the correct default values set by my code) :
➜ ~ 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
I am using Xcode 11.7 (Swift 5) and macOS 10.15.6.
The shared preferences file is corrected created in ~/Library/Group Containers directory, but the defaults command line does not see it.
➜ ~ 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
The same with non-shared preferences works correctly.
Can defaults read Group Containers ?