[User Defaults] Couldn't write values for keys

Hello everyone,

I have problems using SharedUserDefaults with a keyboard-extension.

I could not reproduce the error-message in the simulator, it only appears on real devices.

I created
Code Block language
struct SharedUserDefaults {
static let suiteName = "group.***.MyGroup"
  struct Keys {
    static let addon = "addon"
    static let lastcomment = "lastcomment"
   }
}

Target Membership both my main-app and keyboard-extension.


In the app I load
Code Block language
let sharedUserDefaults = UserDefaults(suiteName: SharedUserDefaults.suiteName)

and save a textfield to it.
Code Block language
if let var_textfield = textfield_addon.text {
      sharedUserDefaults?.set(var_textfield, forKey: SharedUserDefaults.Keys.addon)
    }

I can also load this here, everything works.

Inside my extension I load it with
Code Block language
guard let var_addon = sharedUserDefaults?.string(forKey: SharedUserDefaults.Keys.addon)
        else {return ""}
      return var_addon

This also works in the simulator, but not on my real device.

It always takes the first value I have saved inside the app the first time:


app > save "1" > extension load > "1"
app > save "2" > extension load > "1"


The only hint I have is when saving a value inside the extension I get a message

[User Defaults] Couldn't write values for keys (lastcomment)in CFPrefsPlistSource<0x2817f5b00> (Domain: group.***.MyGroup, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access

Unfortunately I was not able to solve it with this message, as also the online-tutorials do it like my current code.



I am running iOS 14.2.1, I am quite sure that this was working on iOS 13, but unfortunately I have no second device with iOS 13 to test now.


Answered by Claude31 in 651120022
You cannot save outside the sandbox without explicit user permission.

I'm not sure if the app group approach could solve your problem, as it does with watch apps:
https ://www.raywenderlich .com/1950-watchkit-faq

@0x7bf  Pure curiosity: why 1983 ?
Accepted Answer
You cannot save outside the sandbox without explicit user permission.

I'm not sure if the app group approach could solve your problem, as it does with watch apps:
https ://www.raywenderlich .com/1950-watchkit-faq

@0x7bf  Pure curiosity: why 1983 ?
Thank you!

I have just coded this part in a second test-project and I found out, that the error came when I wanted
to write from the extension to the main-app. This also had an effect to the reading, which was actually in a different part of the app.

I don't know why, but this error only came up on the real device, not in the simulator - but now as I know the reason I was able to find a way to solve it in my code :-)

BTW: 1983 my year of birth ;-)
Wish you happy birthday in 0x7E5
[User Defaults] Couldn't write values for keys
 
 
Q