While both my app and extension are able to see and read/write to my shared UserDefaults object, accessed in both cases via:
if let userDefaults = UserDefaults(suiteName: "group.markl.wrshared.container") {
...
}
...they can only read data values that they have written themselves (Strings, in both cases). For example, if I try to read a value from the extension saved by the app:
preferredLanguage = userDefaults.string(forKey: "language")
I always get nil
, but I can read userDefaults
values perfectly well in the extension that were written to userDefaults in the extension. And the same is true going the other way: can't read values from the app that were written by the extension, but can read ones written by the app.
To me it seems obvious that I must be actually writing to different UserDefaults
objects, but I have done many iterations of changing the group name, the App Groups
listing in each target's Signing and Capabilities section, making sure to use UserDefaults.synchronize()
etc. If they really are writing to different UserDefaults
objects I'm at a loss to explain.
I'd be very grateful for any solutions or ideas for how to troubleshoot the problem!