Hello all!
I'm sorry to cross post this question (I've already done on stackoverflow: https://stackoverflow.com/questions/67349440/swifui-quick-actions-using-navigationview-and-navigationlink), but here I hope to find more fortune than there.
Code created so far:
QuickActionViewModel.swift
import Foundation
class QuickActionsViewModel: ObservableObject {
enum Action: Hashable {
case one
case two
case three
}
@Published var action: Action? = nil
}
QATestApp.swift
import CoreData
import SwiftUI
import UIKit
var shortcutItemToHandle: UIApplicationShortcutItem?
let quickActionsViewModel = QuickActionsViewModel()
@main
enum MainApp {
static func main() {
QATestApp.main()
}
}
struct QATestApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.scenePhase) var lifeCycle
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(quickActionsViewModel)
}
.onChange(of: lifeCycle) { newLifeCyclePhase in
if newLifeCyclePhase == .active {
guard let name = shortcutItemToHandle?.type else { return }
switch name {
case "OneAction":
quickActionsViewModel.action = .one
case "TwoAction":
quickActionsViewModel.action = .two
case "ThreeAction":
quickActionsViewModel.action = .three
default:
break
}
}
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) - UISceneConfiguration {
let sceneConfiguration = UISceneConfiguration(name: "Custom Configuration", sessionRole: connectingSceneSession.role)
sceneConfiguration.delegateClass = SceneDelegate.self
return sceneConfiguration
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let shortcutItem = connectionOptions.shortcutItem {
handleShortcutItem(shortcutItem)
}
}
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) - Void) {
shortcutItemToHandle = shortcutItem
}
private func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem, completionHandler: ((Bool) - Void)? = nil) {}
}
ContentView.swift
import Combine
import Defaults
import SwiftUI
struct ContentView: View {
@EnvironmentObject var quickActionsViewModel: QuickActionsViewModel
var body: some View {
NavigationView {
VStack {
Home()
// quick actions links
NavigationLink(destination: Text("One"), tag: .one, selection: self.$quickActionsViewModel.action) { EmptyView() }
NavigationLink(destination: Text("Two"), tag: .two, selection: self.$quickActionsViewModel.action) { EmptyView() }
NavigationLink(destination: Text("Three"), tag: .three, selection: self.$quickActionsViewModel.action) { EmptyView() }
}
}
.navigationViewStyle(StackNavigationViewStyle())
.onReceive(self.quickActionsViewModel.$action, perform: { action in
print(action.debugDescription)
})
}
}
Home() has some stuff to see when the app opens.
I've statically create Shortcut items in the info.plist
keyUIApplicationShortcutItems/key
array
dict
keyUIApplicationShortcutItemIconSymbolName/key
stringbook.fill/string
keyUIApplicationShortcutItemTitle/key
stringOne/string
keyUIApplicationShortcutItemType/key
stringOneAction/string
/dict
dict
keyUIApplicationShortcutItemIconSymbolName/key
stringbook.fill/string
keyUIApplicationShortcutItemTitle/key
stringTwor/string
keyUIApplicationShortcutItemType/key
stringTwoAction/string
/dict
dict
keyUIApplicationShortcutItemIconSymbolName/key
stringbook.fill/string
keyUIApplicationShortcutItemTitle/key
stringThree/string
keyUIApplicationShortcutItemType/key
stringThreeAction/string
/dict
/array
Problem number one:
If I navigate away from home, send the app to background and then use one of the quick actions, none of them work.
I've added
.onReceive(self.quickActionsViewModel.$action, perform: { action in
print(action.debugDescription)
})
to check and every time prints a value
Problem number two:
if I trigger one of the quick actions the app opens and show correctly
the related view. If I send on bg again and trigger another quick
action, the app open to the old one (correct, I sent to background with
this view open), shows the Home(), show the selected view and return to
the home almost istantly, with onreceive printing NIL.
Any help is really appreciated