To create a configurable widget you need to use IntentConfiguration
and IntentTimelineProvider
. This will allow you to access intent parameters which you can later use in the getTimeline
function.
- In your Widget you need to use
IntentConfiguration
(instead of StaticConfiguration
) and select which intent to use (intent
parameter):
struct IntentWidget: Widget {
let kind: String = "IntentWidget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: CustomIntent.self, provider: Provider()) { entry in
WidgetEntryView(entry: entry)
}
.configurationDisplayName("Intent Widget")
.description("Intent Widget")
}
}
- Conform your Provider to
IntentTimelineProvider
(instead of TimelineProvider
). Then you'd be able to use your intent in the getSnapshot
and getTimeline
methods:
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}
func getSnapshot(for configuration: CustomIntent, in context: Context, completion: @escaping (SimpleEntry) -> Void) {
completion(SimpleEntry(date: Date()))
}
func getTimeline(for configuration: CustomIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
// here you can access your intent configuration
// let selectedItem = configuration.selectedItem
...
completion(timeline)
}
}
Useful links:
Here is a GitHub repository with different Widget examples including the Intent Widget.