Get callback from @UIApplicationDelegateAdaptor

I'm using @UIApplicationDelegateAdaptor to detect if the user launched the app by pressing a home screen quick action, like this:
Code Block Swift
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
      if shortcutItem.type == "com.example.ExampleApp.exampleAction" {
        print("Action Pressed!")
      }
    }
    return true
  }
}
@main
struct SwiftUI_AppApp: App {
   
  @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
   
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

This seems to work fine, but how can I notify ContentView that the user had pressed a quick action?

Accepted Reply

There may be some better options, but you can use @Published vars explicitly.
Code Block
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
    @Published var shortcutItemType: String?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem {
            if shortcutItem.type == "com.example.ExampleApp.exampleAction" {
                print("Action Pressed!")
                shortcutItemType = shortcutItem.type
            }
        }
        return true
    }
}
@main
struct SwiftUI_AppApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onReceive(appDelegate.$shortcutItemType) { _ in
                    print(appDelegate.shortcutItemType)
                    //Do something here
                }
        }
    }
}


Replies

There may be some better options, but you can use @Published vars explicitly.
Code Block
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
    @Published var shortcutItemType: String?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem {
            if shortcutItem.type == "com.example.ExampleApp.exampleAction" {
                print("Action Pressed!")
                shortcutItemType = shortcutItem.type
            }
        }
        return true
    }
}
@main
struct SwiftUI_AppApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onReceive(appDelegate.$shortcutItemType) { _ in
                    print(appDelegate.shortcutItemType)
                    //Do something here
                }
        }
    }
}