How to open to a specific view using home quick actions

后端 未结 1 1625
旧时难觅i
旧时难觅i 2020-12-19 18:58

I am new to Swift and have been using SwiftUI, not Storyboard.

I set the UIApplicationShortcutItems in the Info.plist and have two quick actions that are able to pre

相关标签:
1条回答
  • 2020-12-19 19:31

    Here is possible approach by steps (tested with Xcode 11.2 / iOS 13.2)

    1) Add AppSettings class to store mode of views to be presented by shorcut

    class AppSettings: ObservableObject {
        enum Mode {
            case one
            case two
        }
        @Published var mode: Mode? = nil
    }
    

    2) make appSettings scene delegate member to access it and in ContentView and in shortcut delegate and pass it in ContentView as environment object

    let appSettings = AppSettings()
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
        let contentView = ContentView().environmentObject(appSettings)
        // ...
    

    3) activate corresponding mode in shortcut scene delegate

    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        switch shortcutItem.type {
        case "QuickAction1":
            appSettings.mode = .one
            break
        case "QuickAction2":
            appSettings.mode = .two
            break
        default:
            break
        }
    }
    

    4) Make ContentView present links conditionally based on selected shortcut mode

    struct ContentView: View {
        @EnvironmentObject var appSettings: AppSettings
    
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: OneView(),
                                   tag: AppSettings.Mode.one,
                                   selection: $appSettings.mode)
                        { Text("To One") }
                    NavigationLink(destination: TwoView(),
                                    tag: AppSettings.Mode.two,
                                    selection: $appSettings.mode)
                        { Text("To Two") }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题