iOS 14 widget onAppear / didAppear event?

Viewed 702

Is there any technique to re-render iOS 14 widget content when I scroll to my home screen (contain my widget)?

To clarify, this is Home screen of iOS 14 where you can place any widget on. It's not the today extension that appeared in the very left Home screen.

I haven't found any document about this technique! All I know is using Timeline to schedule the rendering, and refreshWidget(widgetKindHere)

Currently, I'm using IntentTimelineProvider to make updating schedule, keep my stock widget up to date frequently for every 30 minutes

But as I can see the TradingView's widget: When I scrolled to my home screen contain its widget, the last updated info on the widget changed from 12:00pm to 2:55pm (current)

-> That mean they did not update their widget from 12pm to 2:30pm, until I scrolled to the Home screen contain the widget, a few seconds later, it get updated at 2:55pm

How can TradingView did that?

1 Answers

This is not about detecting onAppear/onDisappear events. What you observe is the smart behaviour of Smart Stacks. Usually related with the use of TimelineEntryRelevance.

Every TimelineEntry has a TimelineEntryRelevance. The relevance is required, however, the default implementation is provided.

struct SimpleEntry: TimelineEntry {
    var date: Date
    var relevance: TimelineEntryRelevance?
}

let entry = SimpleEntry(date: Date(), relevance: 0.1)

Why relevance is important? It is used by Smart Stacks to display the most relevant widget, i.e., the one with the most relevant information.

From Welcome to Design Great Widgets:

Another feature we've created is Smart Stacks. These let you add several different widgets into a single location and quickly flick between the different widgets inside. But what is really powerful is that smart stacks dynamically change and adapt to how you use them. Based on your behavior and context, a smart stack will automatically rotate to show you the most relevant widget at a given time. [emphasis added]

From Add configuration and intelligence to your widgets:

You want your widget to be surfaced when you have timely information relevant to the people using your app, like when you know there is a thunderstorm coming, but not when you're just updating the temperature regularly.

For example, if someone frequently launches your weather app to check on the weather, the system can instead surface your widget with that information to provide quicker access to what they're looking for.

The second is based on relevant information from your app. So, for example, if a thunderstorm is happening, your widget can inform the system that it has a highly relevant update, and the system will consider surfacing your widget to the top of the stack.


Useful links:

Related