Since I've updated from Xcode 10.1 to 10.2, I've got a lot of issues where release builds were behaving different as debug builds. In most of those cases, some functions weren't invoked at all on the release build where they would be on debug builds.
Now I've got this following issue, which I can reproduce with a simple new project (to be sure the issue isn't caused by any added framework or something):
I can't retrieve data from a UserDefaults group from a Today Extension on release build.
I created a new project and registered an app group `group.mydomain.test`.
In the AppDelegate I added:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let shared = UserDefaults(suiteName: "group.mydomain.test")
shared?.set("MyTestValue", forKey: "MyTestKey")
print("Shared value for 'MyTestKey': \(shared?.string(forKey: "MyTestKey") ?? "nil")")
return true
}
Which correctly prints the value being set.
Then I added a Today Extension, replaced the 'Hello world' label with a new label which I linked to the view controller.
In TodayViewController I added:
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
let shared = UserDefaults(suiteName: "group.mydomain.test")
self.testLabel.text = (shared?.string(forKey: "MyTestKey")) ?? "nil"
completionHandler(NCUpdateResult.newData)
}
When I run the app as a `debug` build, everything works fine and the extension will show a label with "MyTestValue".
When I run the app as a `release` build, the extension will show "nil".
I went to the apps container folder in the simulator and I do see the group's plist in preferences and it does contain the set value.
Even when I change all build settings of the release to match the debug settings (e.g. optimization), it still doesn't work.