Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

ios - How to navigate from widget extension to UIKit viewController?

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)
    }
}

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

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 ;)

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...