6

I'm currently creating my first widgets for my applications. The data is obtained via an API call. I would like to know if it is possible to make this request from the application and then send the result of this request to the widget ? (a bit like WatchConnectivity does for the watch)

Thanks for your help :-)

jackson89
  • 163
  • 2
  • 7
  • This might help you: [How to refresh Widget data?](https://stackoverflow.com/questions/63976424/how-to-refresh-widget-data) – pawello2222 Sep 21 '20 at 23:15
  • Does this answer your question? [Share data between main App and Widget in SwiftUI for iOS 14](https://stackoverflow.com/questions/63922032/share-data-between-main-app-and-widget-in-swiftui-for-ios-14) – pawello2222 Nov 05 '20 at 21:40

2 Answers2

4

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.

Artem Kirillov
  • 1,132
  • 10
  • 25
  • 1
    Interesting but if your API requires say an access token, this needs to come from your app to the widget so it can make that API call so we are back at the communication question between app and widget... – Muvimotv Oct 05 '22 at 00:00
2

Yes this is possible using one of the WidgetCenter APIs to reload your timeline.

...
// make API call
// store data in shared storage that the Widget uses


WidgetCenter.shared.reloadAllTimelines()
// OR
WidgetCenter.shared.reloadTimelines(ofKind: "WidgetKind")

Note that it's most likely preferred to use reloadTimelines(ofKind: "WidgetKind") since it will only reload the timelines of a specific widget. "WidgetKind" can be found in your WidgetConfiguration definition

Taylor Johnson
  • 1,845
  • 1
  • 18
  • 31
  • 9
    I am not sure this accepted answer really answers this question. Or at least what I understand from the question is that the person request a data via an API call, then he wants to send that data from the main app to the widgetExtension so that he can display it on his widget. – J Arango Aug 14 '20 at 20:18
  • 1
    This would handle that case as long as the developer stores the data from the API call in shared storage using App Groups. Widgets aren't constantly running processes, so I don't believe there is a way to "send" the data from the app to the widget without using shared storage – Taylor Johnson Aug 19 '20 at 19:17