onContinueUserActivity not working on first app launch

HI,

I'm trying to deeplink from my widget to a view in my app. I'm using the "SwiftUI app lifecycle".

This the code I'm currently using:

var body: some Scene {
        WindowGroup {
            RootView()
                .onContinueUserActivity("NextDeparturesWidgetConfigurationIntent") { userActivity in
                    guard let configuration: NextDeparturesWidgetConfigurationIntent = userActivity
                        .widgetConfigurationIntent(),
                        let stationEntity = configuration.stationEntity else { return }
                    NotificationCenter.default.post(name: .onOpenStation, object: stationEntity.id)
                }
        }
    }

It is working fine when the app is already running (in the background). However when the app is "cold starting" (ie. not in memory) onContinueUserActivity is never called.

I tried adding a UIApplicationDelegateAdaptor:

    func application(
        _ application: UIApplication,
        configurationForConnecting connectingSceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        if let userActivity = options.userActivities.first {
            let configuration: NextDeparturesWidgetConfigurationIntent? = userActivity.widgetConfigurationIntent()
            // casting fails
        }

        return UISceneConfiguration(
            name: nil,
            sessionRole: connectingSceneSession.role
        )
    }

I can see that the activity is received but it is never casted to NextDeparturesWidgetConfigurationIntent.

What am I missing ?

Answered by Engineer in 798403022

Hi,

In your SwiftUI View

.onOpenURL(perform: { (url) in

Should be called in response to the

.widgetURL(entry.character.url)

set on your Widget's SwiftUI view.

You can see the Emoji Rangers sample project for additional information

Rico WWDR - DTS - Software Engineer

Hi,

In your SwiftUI View

.onOpenURL(perform: { (url) in

Should be called in response to the

.widgetURL(entry.character.url)

set on your Widget's SwiftUI view.

You can see the Emoji Rangers sample project for additional information

Rico WWDR - DTS - Software Engineer

Hi, thank you for your answer. I tested with an URL and this is indeed working well. I think this is a nice workaround but that means I will have to somehow encode my WidgetConfigurationIntent to the URL before sending it.

While this is a nice workaround I would like to know what is the purpose of userActivity.widgetConfigurationIntent(). Is it broken ?

I made a small sample here with two commits:

  • First commit is using the WidgetConfigurationIntent, we can see it's working when the app is already running.
  • Second commit is using the widgetURL we can see it's working well but it is also breaking the userActivity.widgetConfigurationIntent()

https://github.com/PhilippeWeidmann/ContinueUserActivityWidget

I've been experiencing this exact issue as well. Using .widgetURL() is very cumbersome, as in my case I'm letting users configure parameters of type Measurement<UnitMass>. This type safety would be lost if serialised to a string. It feels like a bug in the handling of userActivity <> Intent since it works as intended only for running state of the app.

onContinueUserActivity not working on first app launch
 
 
Q