如何从小部件扩展导航到 UIKit viewController? [英] How to navigate from widget extension to 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
可以有,例如</p>
and AppState
can have, for example
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屋!