'Data written to defaults within an unwanted communications extension isn't persisted
I'm using 4 extensions within my app and use a group in combination with UserDefaults.init(suiteName:) to share settings between the extensions and the app.
However I've just tried adding an unwanted communications extension and found that data writing to the defaults, using the exact same way as its written in the other extensions, isn't saved. At first I noticed data written by the UCE wasn't present when the app tried to read it, so performed an experiment and found that while the extension is running it can write data to user defaults and read it back, but the next time the extension runs, all that data has gone.
I've tried using the old UserDefaults.synchronize() method after writing the data but that makes no difference.
Why is the UC extension different from every other extension? Is it possible to write and persist data from within it?
let groupName = "group.com.mycompany.appName"
let sharedDefaults = UserDefaults.init(suiteName: groupName)
var theValue = sharedDefaults!.value(forKey: "some key")
NSLog("\(theValue)") // prints nothing, despite the extension having previously run
sharedDefaults!.set("some value", forKey: "some key"))
sharedDefaults!.synchronize()
theValue = sharedDefaults!.value(forKey: "some key")
NSLog("\(theValue)") // prints "some value"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
