How to navigate from widget extension to UIKit viewController?

前端 未结 2 1451
醉酒成梦
醉酒成梦 2020-12-21 16:50

I am currently trying to make a widget with SwiftUI. Let\'s say my code is like this:

struct WidgetView: View {
    var data : DataProvider.Entry
    var body         


        
相关标签:
2条回答
  • 2020-12-21 17:03

    Here is possible approach

    var body: some Scene {
        @StateObject private var appState = AppState()
    
        WindowGroup {
            ContentView()
                .environmentObject(appState)
                .onOpenURL(perform: { url in
                    appState.handle(url)       // redirect to app state
                })
        }
    }
    

    and AppState can have, for example

    class AppState: ObservableObject {
        @Published var appScreen: _SomeEnumType_
    
        func handle(url: URL) {
            // ... update `appScreen` here correnspondingly
        }
    }
    

    so inside ContentView depending on app state you can switch shown views.

    0 讨论(0)
  • 2020-12-21 17:26
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    
    }
    

    I got this method performed in SceneDelegate when I clicked the widget. You can handle your url and redirect to the ViewController

    0 讨论(0)
提交回复
热议问题