Timeline Providers and Snapshots
Supply widget content over time.
The Provider's Job
A widget never updates itself continuously. Instead, a TimelineProvider hands WidgetKit a schedule of pre-rendered entries, and the system displays each at the right time. The provider answers three questions: placeholder, snapshot, and timeline.
import WidgetKit
// TimelineProvider supplies:
// placeholder(in:) -> instant skeleton
// getSnapshot(in:) -> one entry for previews
// getTimeline(in:) -> future entries + refresh policyConforming to TimelineProvider
A provider conforms to TimelineProvider with an associated Entry type. You implement the three methods; each gives you a Context describing the family and whether it is a preview.
import WidgetKit
struct WeatherProvider: TimelineProvider {
typealias Entry = WeatherEntry
func placeholder(in context: Context) -> WeatherEntry {
WeatherEntry(date: Date(),
temperature: 20, condition: "Sunny")
}
// getSnapshot and getTimeline follow
}All lessons in this course
- Building a WidgetKit Widget
- Timeline Providers and Snapshots
- App Extensions Overview
- App Intents and Shortcuts