Generally widgets are not supposed to receive any data directly from the app. Widgets have got a concept of TimelineProvider
which generates a timeline consisting of timeline entries. Each entry specifies the date and time to update the widget’s content and the content itself. If content needs to be fetched from the server, you can use standard URLSession
API in the methods of your timeline provider, and attach the data to your timeline entry. That’s why timeline provider has got completion handler as parameter in its method:
func getTimeline(in: Self.Context, completion: (Timeline<Self.Entry>) -> Void)
WidgerCenter
API (recloadAllTimelines()
or reloadTimelines(ofKind:)
) is supposed to be used to inform WidgetKit
when a time line changes. For example, when user changes something in the main app, sends request to the backend, updates data base with new info, you need to initiate widget update as well to fetch updated data and refresh.