iOS 14 widget UserDefaults(suiteName:) not working

Viewed 645

Xcode 12.0 (12A7208) MacOS 10.15.6 (19G2021)

  1. Create an new SwiftUI project.
  2. Add Capability App Groups.
  3. Create an new Target Widget Extension, add Capability App Groups.
  4. In Main App use UserDefaults(suiteName:).set to save an Int data.
  5. In Widget, Under getTimeline(for:in:completion:) function just call UserDefaults(suiteName:).value,cannot get the Int Data. But in Main App can.

suiteName is started with group., and it is same in Widget and Main App.

quite confused...

1 Answers

For any future readers I found this error - "Code Signing Error - Verify the value of the CODE_SIGN_ENTITLEMENTS" which was in a very un-obvious place.

To fix:

  1. Go to your new Target
  2. Go to Build Settings
  3. Go to the Signing section. Make sure you have an .entitlement file assigned to both Release and Debug. An entitlement file should've been automatically created in your folder structure. You just need to make sure you get the path correct eg. "Widget/WidgetExtension.entitlement"
Related