WidgetKit - custom dynamical intent - defaults?

I followed this guide

https://developer.apple.com/documentation/widgetkit/making-a-configurable-widget

to set up custom dynamical intents.

That works fine, but: how can I set a default for an intent which is used when the user adds a widget to the home screen?

In such a case the user did not select an intent yet, and in my specific situation, parts of the widget are empty as they rely on the intent.

That means that I need to find a way to specify a default for each custom dynamical intent, which are applied to the widget right when added to the home screen.

Thanks for any help. :o)

Replies

I have the same issue ... I've spent hours on it, tried to set the default values in different places:

in the Provider struct:
  • in func placeholder(in context: Context),

let configuration = SelectDataItemIntent()
configuration.dynamicParam = [DynamicParam], size depending on context.family
  • in func getSnapshot(for configuration: 

if configuration.myDynamicParam == nil { ... }
  • in func getTimeline(for configuration:

if configuration.myDynamicParam == nil { ... }

That works fine for the preview and for the widget itself, but unfortunately the set parameters as not saved.
Therefore the EditWidget UI shows 'select' for each parameter instead of the expected value .. not very nice ...

I've also tried to initialize it in the Widget struct, but the Intent configuration requires an instance type of the Intent, not an actual instance, so it seems not possible from there...

Have you find any solution ?


For parameters that have a known set of options (boolean, String, enum, etc) you specify the default in the intent definition file when you specify the parameter in the first place. When a snapshot is generated for preview or when the user initially installs a widget on their homes screen, the default value is the way the intent is initialized, and that is the value you will receive in the configuration that is sent to your snapshot(...) and timeline(...) implementations. The one wrinkle here is that if the widget was installed using an older version of your app (ie you add a new configuration option in a later release) that value will not get initialized and will be nil until the user opens the configuration. I believe this is a bug (filed as FB8754457 if anyone at Apple is listening). The way to deal with that is to look for that nil value and replace it with whatever you want the default to be. I do this in an extension to the intent like the following:

Code Block
extension MyIntent {
    public func me_showThing() -> Bool {
        return self.showThing?.boolValue ?? true
}
}


If you are trying to specify the default value for a dynamic parameter then you do that in the same place you specify the list of possible values. Once you specify that a parameter values are dynamic then a protocol is generated that is something like MyIntentHandling which has two relevant methods: one is for specifying all the options of the form provideMyThingOptionsCollectionForMyIntent:withCompletion: and one to specify the default that is something like defaultMyThingForMyIntent:
Thank you jcgoforth, the 'defaultMyThingForMyIntent:' is indeed the right place.
Thanks !