user298519
user298519

Reputation: 1190

SwiftUI Home Screen Quick Actions: Trying to navigate to the DetailView of the first List item using a quick action

I have a list of sports events in my app. I want the user to be able to navigate to the nearest upcoming event using a quick action. When I do, I am navigated to the homepage.

enum QuickAction: String {

  case firstView = "com.myappname.event-watcher1.firstItem"
  case secondView = "SecondView"

}

enum QA: Equatable {

  case firstView
  case secondView

  init?(shortcutItem: UIApplicationShortcutItem) {
    
      guard let action = QuickAction(rawValue: shortcutItem.type) else {
          return nil
      }
    
      switch action {
      case .firstView:
          self = .firstView
      case .secondView:
          self = .secondView
      }
  }
}

class QAService: ObservableObject {

    static let shared = QAService()
    @Published var action: QA?
 
}

AppDelegate.

class AppDelegate: NSObject, UIApplicationDelegate {
    private let qaService = QAService.shared
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        if let shortcutItem = options.shortcutItem {
            qaService.action = QA(shortcutItem: shortcutItem)
        }
        let configuration = UISceneConfiguration(name: connectingSceneSession.configuration.name, sessionRole: connectingSceneSession.role)
        configuration.delegateClass = SceneDelegate.self
        return configuration
    }
}

AppName_App.swift

@main
struct Event_WatchApp: App { 
    
    private let qaService = QAService.shared
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    init() {
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(qaService)
        }
    }
}

Upvotes: 0

Views: 63

Answers (0)

Related Questions