I'm creating a configurable widget using AppIntentConfiguration in my SwiftUI app and wanted to read configuration's payload when the user taps on the widget and launches the app.
Having read the WidgetKit's documentation I noticed I can just call userActivity.widgetConfigurationIntent(of: MyWidgetConfigurationAppIntent.self) inside .onContinueUserActivity() modifier, to get the intent's instance.
This function works and returns the instance when user taps on the widget and the app is already running in the background, but returns nil when the app launches for the first time.
Am I doing something wrong here, is this a desired behaviour? Is using Deep Links a more suited solution for this use case? I'm really not liking the idea of serialising instances of Measurement and UnitMass/UnitTemperature into URLs.
Here's a sample code to illustrate:
@main
struct WidgetIntentTestApp: App {
@State private var favouriteEmoji: String?
private let intentActivityName = String(describing: ConfigurationAppIntent.self)
var body: some Scene {
WindowGroup {
ContentView(favouriteEmoji: favouriteEmoji)
.onContinueUserActivity(intentActivityName) { userActivity in
guard let intent = userActivity.widgetConfigurationIntent(of: ConfigurationAppIntent.self) else {
/// Intent is `nil` when the user activity `launches the app for the first time`.
/// I would have expected this to work given the user activity's `.name` clearly matches the Intent's name
fatalError("Expected intent but received `nil` - this should not have happened.")
}
favouriteEmoji = intent.favoriteEmoji
}
}
}
}