Create a widget template added in iOS14 with Intent Configuration. (Those who can see the "Edit Widget" menu) Use Xcode auto-generation whenever possible. Since it is a template, it has no function, but I hope it will be the basis for mass production of widgets.
I think most of the apps are still made with UIKit, so I chose ʻUIKit App Delegate. The name is LeaderCard` assuming a credit card application. : relaxed:

Press + here.

Select Widget Extension and press Next.

ʻCheck Include Configuration Intent. (← important here!) If you do not check it, it will be Static Configuration. The Widget name is LeaderCardWidget`.

Press + here.

Select Intents Extension and press Next.

ʻDo not check Include UI Extension. (← important here!) Select None for Starting Point. (← important here!) The Intent Handler name is LeaderCardIntent`.

Open the LeaderCardWidget.intentdefinision generated in the LeaderCardWidget folder.
Press + at the bottom to select New Type.
Set Card to the type name. You don't have to touch the contents of Card.
Parameters
Set as shown in the figure below.

Parameter
Parameter is set to card in lowercase. (← important here!)
This is because it is the property name of the automatically generated class ConfigurationIntent.
Display Name
The character string specified here is displayed in the location shown in the figure below.

Type
Select Card.
Configurable To check.
Dynamic Options To check.
Prompt Label
The character string specified here is displayed in the location shown in the figure below.

Target Membership
Check LeaderCardWidgetExtension and LeaderCardIntent. (← important here!)

initial state
IntentHandler.swift
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
return self
}
}
Add the ConfigurationIntentHandling protocol.
Then you will be asked if you want to add a protocol stub, so press Fix.
The provideCardOptionsCollection method is added.

Write completion (nil, nil) in the code part.
(Since it is a template, nothing works.)
IntentHandler.swift
class IntentHandler: INExtension {
func provideCardOptionsCollection(for intent: ConfigurationIntent, with completion: @escaping (INObjectCollection<Card>?, Error?) -> Void) {
completion(nil, nil)
}
override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
return self
}
}
The template should now be complete.
When executed, the following screen will be displayed.
We will flesh out this and get closer to the desired widget.

GitHub
We have created a Template repository here, so please use it if you like.
iOS14-Widget-IntentConfiguration-template
Recommended Posts