2

I have an app inlcuding an Apple Watch app and a WidgetKit (iOS 16) extension.

The iOS app sends data via WatchConnectivity to the Apple Watch app and displays it there. Also it provides data to a WidgetKit (iOS 16 - Lockscreen) extension using GroupUserDefaults. All of this works without any issues ✅.

My issue:

I'm now trying to supply data to a WidgetKit complication extension (WatchOS 9). I expected to be able to either access the previously written UserDefaults of the Apple Watch app or the shared GroupUserDefaults. But I can't access any of it's data. I also checked the documentation, but I can't find any methods explicitely designed to share data to the complications. ClockKit methods will not be available for WidgetKit complications (https://developer.apple.com/documentation/widgetkit/converting-a-clockkit-app).

Did anybody manage to share data to a WidgetKit complication without requesting a Server API like in the Apple example? (https://developer.apple.com/documentation/widgetkit/adding_widgets_to_the_lock_screen_and_watch_faces)

Simon C.
  • 1,605
  • 14
  • 13

1 Answers1

3

So I can answer my own question:

App Group shared UserDefaults are available only on the device itself it seems. Therefore it's necessary to enable the App Group capability on the WidgetKit extension of the Watch app as well as on the Apple Watch app. The Watch App can then write the data recieved through WatchConnectivity to the App Group shared UserDefaults from where the WidgetKit extension can access it.

So if you encounter any issues, check the capabilites of both targets and also profiles and identifiers, if you manage certificates manually/fastlane.

Simon C.
  • 1,605
  • 14
  • 13
  • Hi Simon, I saw the WWDC video where they said we need to implement `recommendations` method to pass the `IntentRecommendations`. In my case, I have stored the data in iOS Keychain. I'll share the data to the Watch app by WatchConnectivity and store in watch keychain. For, I've enabled the app group capability for watch app, widget, watch widget targets. Still, I couldn't get the value! Should I pass the data (to be shown in complication) to the watch app before the `recommendations` method? – Azhagusundaram Tamil Aug 29 '22 at 13:41
  • I don't work with intents, as they represent any intent of the user, which is not the case for my app. Did you save & synchronize the data to the Group UserDefaults after receiving it via WatchConnectivity? – Simon C. Aug 30 '22 at 16:10
  • Actually, I'm storing the data in keychain; not in the user defaults. – Azhagusundaram Tamil Sep 01 '22 at 07:30
  • Your answer is working I can get the data in watch widget complication by storing the data in Keychain. Data is shared by WatchConnectivity. Thank you – Azhagusundaram Tamil Sep 01 '22 at 09:41
  • Hello! @SimonC! I was facing the same problem yesterday, but managed to fix it exactly the same way you did! However, I'm still having issues with widgets not updating on watch. Could you please share how you trigger WidgetKit complications timeline reload? You can see full story here: https://stackoverflow.com/questions/76865886/widgetkit-complications-wont-update – Serzhas Aug 09 '23 at 10:36
  • @Serzhas you have only a couple of syncs per day, I think around 70. Maybe that's the issue? I also have difficulties distributing them over the day. – Simon C. Sep 02 '23 at 16:58