Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserDefaults(suiteName:) not propagating between app and extension

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!

like image 216
Mark Avatar asked Oct 15 '25 03:10

Mark


1 Answers

To get the desired behaviour the App Group must be the same for both the app and the extension, and you must use that name as suiteName.

like image 132
storoj Avatar answered Oct 17 '25 18:10

storoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!