Jongwoo Lee
Jongwoo Lee

Reputation: 1128

How do I start my SwiftUI app in a children view?

Okay, here's what my app looks like. View Hierachy

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView()
            }
        }
    }
}

And this is MemorizableApp.swift. As you can see, the app starts in MainView. But I want the app to launch in NotebooksView instead. Obviously, changing MainView to NotebooksView in the code above won't work because 1) NotebooksView doesn't have a NavigationView in it, and 2) I won't be able to navigate back to MainView. But somehow Apple's Shortcuts app did exactly this without creating a custom back button. How can I achieve this behaviour?

Upvotes: 0

Views: 373

Answers (1)

RelativeJoe
RelativeJoe

Reputation: 5084

What you need to do is activate your NavigationLink programmatically:

In MainView add: @State var shouldNavigate = false, then modify your NavigationLink to the following:

NavigationLink(destination: NoteBooksView(), isActive: $shouldNavigate) {
    ...
}

And in MemorizableApp:

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView(shouldNavigate: true) //set it to true
            }
        }
    }
}

Note: The use of NavigationLink(destination: Content, isActive: Binding<Bool>) & NavigationView is deprecated in iOS 16. Take a look here & here.

Upvotes: 1

Related Questions