cocoansuserdefaultsappstore-sandboxappgroups

Persisting standard NSUserDefaults with App Groups enabled


Original situation:

I'm developing a sandboxed app on macOS 10.13. It's a shoebox-like app consisting of a main window with toolbar. Autosaving for the window frame and toolbar configuration are set up in Interface Builder.

If I run and quit the app, I can see a preferences file (named <app-identifier>.plist) containing the expected data in the app's sandbox container. Everything works fine.

Extending the app with App Groups:

To allow data exchange with other sandboxed apps, I created an app group in my target's Capabilities tab of Xcode.

So far this works, i.e. an additional group container is created when launching the app. It is also possible to store shared preferences into the group container by creating a UserDefaults object via UserDefaults.init(suiteName:"<group-name>").

The problem:

After adding the App Group, the autosaving mechanism for e.g. the toolbar configuration no longer works. Starting and quitting the app on a clean Mac no longer creates a .plist file in the Preferences directory of the app container. Instead the console shows messages like this:

[User Defaults] attempt to set <value> for key in <key> in
non-persistent preferences domain CFPrefsPlistSource<address>
(Domain: <app-identifier>, User: kCFPreferencesCurrentUser,
ByHost: No, Container: (null), Contents Need Refresh: Yes)

It looks for me, that the autosaving mechanism is no longer able to write its data into the preferences file.

Is it possible to concurrently use standard UserDefaults and shared ones in the app groups?


Solution

  • Problem solved: after a rebooting the Mac, everything works as expected. This is really weird.