I want to read UserDefaults when a user changes the configuration for a widget, so I created an app group and added the capabilty to my main app, the widget extension and the Intent extension.
I can read and write fine between my app and the widget extension, however the UserDefaults always return nil
in IntentHandler.swift, what could cause this?
// called from TimelineProvider
func example() -> String {
let sharedDefaults = UserDefaults.init(suiteName: "group.myname")
let value = sharedDefaults!.value(forKey: "myKey") as! String
// value == “Hi this works!”
}
But when I call example()
from my IntentHandler value is nil
. (It crashes)
As EmilioPelaez commented, double check the capabilities (even if you've already done so, I didn't notice this either). My app group was only enabled for release mode, not debug. Should have noticed this sooner...