4

I have a widget with a .intentdefinition file set up, and I can select from my enum when running my widget, but I'm not sure how to use this information in the code.

What I want to be able to do is run different code in getTimeline based on what the user selects in the widget settings.

If anyone could help, that'd be greatly appreciated. Thanks!

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Noah Evans
  • 309
  • 3
  • 12

1 Answers1

4

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.

  1. 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")
    }
}
  1. 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.

pawello2222
  • 46,897
  • 22
  • 145
  • 209