Building widgets with WidgetKit

Modern platform Mid-level Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

After the user edits a list in the app, the widget still shows old data for hours. Fix?

Options
Hint 1 of 1

Who tells the widget the data changed?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Write the data to the App Group and call WidgetCenter.shared.reloadTimelines(ofKind:)

Correct. The widget reads fresh data and asks for a new timeline.

Why not the others?
  • B Add a Timer in the widget view. Widgets don't run timers; they render entries.
  • C Set the reload policy to every second. Reloads are budgeted, and the system won't honour that.
  • D Reinstall the widget. Users shouldn't need to.

30-second answer

A widget is a SwiftUI view rendered from a timeline of entries that your TimelineProvider supplies; the system shows each entry at its date, so the widget doesn't run code continuously. Share data with the app through an App Group, call WidgetCenter.shared.reloadTimelines(ofKind:) when the data changes, and choose a reload policy. Since iOS 17, widgets can be interactive with Button(intent:) and Toggle(intent:).

Practise saying it · 45 s. No recording, just a timer

Explain how widgets update.

Say your answer out loud, then check which of these points you covered:

  • Timelines
  • App Group data
  • reloadTimelines
  • Interactive intents

Deep dive

Reloads are budgeted; don't reload on every small change.

Use containerBackground (required from iOS 17) and support every family you declare.

AppIntentConfiguration lets users configure widgets.

Widgets can appear on the Lock Screen, StandBy and Control Center (controls, iOS 18).

Common mistakes

  • Expecting a widget to run timers or live code.
  • Forgetting to reload timelines after app changes.

Senior take & follow-ups

Seniors explain the timeline model and the refresh budget.

Follow-up questions

Think of your answer first, then open the hint.

  1. How does an interactive widget run code?

    Show hint

    Through an AppIntent's perform().

  2. Why use an App Group?

    Show hint

    The widget extension runs in its own sandbox.

Suggest a fix if something here is wrong or out of date.