Sharing UserDefaults with widgets in iOS 14

I'm unable to share UserDefaults with the same suiteName with newly announced iOS 14 widgetkit extension (It constantly returns nil). Is there anything that I should do apart from having same suite name for this to work? Thanks!

Replies

Seems to be working for me, did you make sure to also add the App Group to the WidgetKit Extension?
Post not yet marked as solved Up vote reply of ay8s Down vote reply of ay8s
Ay8s thanks.
I thought the appgroup added automatically to both targets, but instead you have to do it manually for the app extension.

Instead can you tell me something about passing data with the FileManager between Widget extension and main app?
Thanks

same issue,
URL by create with
Code Block
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: 'appGroupID')
works, not work with
Code Block
UserDefaults(suiteName: 'suiteName')

UserDefaults works fine for me. Note that the suitName must be the same as your app group id.
I am having the same problem and am unable to pass UserDefaults with the same registered suite name from my main app to my iOS 14 Widget Extension. The UserDefaults (with the same suite name) are working properly in my main Objective C app, but are returning nil in the SwiftUI Widget extension. The UserDefaults AppGroups are included in both targets. I am new to SwiftUI and have probably done something wrong, but I have no clue what the issue may be. Does anyone have any suggestions?
I solved the problem I was having. It turns out I was using an incorrect App Group identification.
For me, the only working way is to use
Code Block
FileManager.default.containerURL

User Defaults doesnt work.
This article gives a good walkthrough of sharing data with a widget using UserDefaults and App Groups:
medium.com/better-programming/sharing-object-data-between-an-ios-app-and-its-widget-a0a1af499c31
I can use App Groups and UserDefaults with Widgets perfectly on the simulator and on the iPhone (as Simulator). But as soon as I publish the App on TestFlight, the Data disappears. Nothing is saved in UserDefaults and I do not get the Widgets Data... Nevertheless, on the simulator it works perfectly! What am I doing wrong? Please help!

Thanks

But as soon as I publish the App on TestFlight, the Data disappears.

This sounds like an entitlement issue. On iOS your App Group access is encoded as an entitlement in your code signature and that entitlement must be allowlisted by your provisioning profile. If the entitlement is missing, you won’t be able to access your App Group.

A good way to debug this is export a Distribution-signed version of the app using the Organizer (Distribute App > App Store Connection > Export, instead of Upload). You can unpack the resulting .ipa by renaming it to .zip, and then dump your entitlements using:

Code Block
% codesign -d --entitlements :- /path/to/your.app


Also dump the entitlements of your Widget app extension (the .appex inside PlugIns) to confirm that they’re aligned.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi @eskimo - Found this post and experiencing the same issues as above. When I do the entitlements dump, I am seeing Warning: Specifying ':' in the path is deprecated and will not work in a future release I doubt this is an issue right now..

Hopefully your tip will help me debug my issue :)