2020 SwiftSwiftUI & UI FrameworksApp Services
WWDC20 · 23 min · Swift / SwiftUI & UI Frameworks / App Services
Meet WidgetKit
Meet WidgetKit: the best way to bring your app’s most useful information directly to the home screen. We’ll show you what makes a great widget and take a look at WidgetKit’s features and functionality. Learn how to get started creating a widget, and find out how WidgetKit leverages the power of SwiftUI to provide a stateless experience. Discover how to harness your existing proactive technologies to make sure your widget surfaces relevant material. And create a Timeline that ensures your content is always fresh. For more on creating widgets, check out "Build SwiftUI views for widgets" and "The widgets code-along."
Watch at developer.apple.com ↗Code shown on screen · 4 snippets
StaticConfiguration Widget definition
@main
public struct SampleWidget: Widget {
private let kind: String = "SampleWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind,
provider: Provider(),
placeholder: PlaceholderView()) { entry in
SampleWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
} TimelineProvider example
public struct Provider: TimelineProvider {
public func snapshot(with context: Context,
completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
public func timeline(with context: Context,
completion: @escaping (Timeline<Entry>) -> ()) {
let entry = SimpleEntry(date: Date())
let timeline = Timeline(entries: [entry, entry], policy: .atEnd)
completion(timeline)
}
} IntentConfiguration Widget definition
@main
public struct SampleWidget: Widget {
private let kind: String = "SampleWidget"
public var body: some WidgetConfiguration {
IntentConfiguration(kind: kind,
intent: ConfigurationIntent.self
provider: Provider(),
placeholder: PlaceholderView()) { entry in
SampleWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
} IntentTimelineProvider example
public struct Provider: IntentTimelineProvider {
public func timeline(for configuration: ConfigurationIntent, with context: Context,
completion: @escaping (Timeline<Entry>) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
// generate a timeline based on the values of the Intent
completion(timeline)
}
} Resources
Related sessions
-
18 min -
11 min -
27 min -
20 min -
9 min -
15 min -
9 min -
28 min -
12 min -
16 min -
15 min -
28 min -
27 min