如何从小部件扩展导航到UIKit viewController? [英] How to navigate from widget extension to UIKit viewController?

查看:70
本文介绍了如何从小部件扩展导航到UIKit viewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用SwiftUI制作小部件. 假设我的代码是这样的:

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: some View {
        ZStack {
            Image("Clicker")
                .padding(.all, 15)
        }.widgetURL(URL(string: "ClickerViewController"))
        .padding(.all, 30)
        .background(Color.green)
    }
}

目前,点击窗口小部件将打开应用程序的主屏幕.想要导航到特定屏幕. 支持的家庭:. systemSmall -因此需要使用. widgetURL 而不是.Link

At the moment, tapping the widget opens the home screen of the app. Want to navigate to a specific screen. supportedFamilies: .systemSmall - so need to use .widgetURL and not .Link

那么在URL是Clicker的地方,如何导航到名为ClickerViewController的viewController? 如何处理这些链接?我猜,任何示例或解释都将在不久的将来对我和很多其他人有所帮助;)

So where the URL is Clicker, how to navigate to viewController that is called ClickerViewController? How to handle these links? Any examples or explaining would help me and a lot of others in the near future I guess ;)

谢谢!

推荐答案

这里是可行的方法

var body: some Scene {
    @StateObject private var appState = AppState()

    WindowGroup {
        ContentView()
            .environmentObject(appState)
            .onOpenURL(perform: { url in
                appState.handle(url)       // redirect to app state
            })
    }
}

例如

AppState可以具有

class AppState: ObservableObject {
    @Published var appScreen: _SomeEnumType_

    func handle(url: URL) {
        // ... update `appScreen` here correnspondingly
    }
}

因此在ContentView内部,根据应用程序状态,您可以切换显示的视图.

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

这篇关于如何从小部件扩展导航到UIKit viewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆