Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WidgetKit with dynamic data, how to specify the default IntentConfiguration for new widgets?

The desirable example is exhibited by the Apple Notes widgets. When adding an Apple Notes widget, WidgetKit goes ahead and assigns an IntentConfiguration to the new widget. You can see this by editing the widget and noticing that the selection element does not read "Choose" but instead includes the name of the note or folder that was automatically selected as default for the new widget. You'll also notice that this placeholder widget's data source does not change from one source to another – the assigned IntentConfiguration is respected.

How can we do this in our Widgets? It should look like this: someone creates a new widget, we use their most recent data item for the placeholder, and that item is 1) persisted in the Widget until they change it 2) reflected in the edit widget dialogue.

like image 911
AVS Avatar asked Oct 03 '20 15:10

AVS


1 Answers

I was looking for same, trying to set a default for the dynamic type, so idea is set a default just like you can with enums in the intent definition file. short answer we need to set the dynamic type default in code.

so after some digging around came across this(I command clicked on my ConfigurationIntent reference in my code and got to apples auto generated ConfigurationIntent.swift)

/*! @abstract Default values for parameters with dynamic options
 @discussion Called to query the parameter default value.
 */
@available(iOS 14.0, macOS 10.16, watchOS 7.0, *)
@objc(defaultChartTypeForConfiguration:)    
optional func defaultChartType(for intent: ConfigurationIntent) -> Chart?

here we have "optional fund defaultChartType" so I've implanted that in the intenthandler.swift that will be generated for you in which you then have to add code to retrieve and return dynamic types. that should be covered in any tutorial on displaying dynamics in the widget configuration screen. so now here in this file too I've just added the optional func above. ChartType is my dynamic type I created in the intentDefinition file. yours will be different to match your names

and its working this way. hope that helps and you can follow it.

like image 106
Kio Avatar answered Nov 05 '22 09:11

Kio