Cameron Henige
Cameron Henige

Reputation: 416

How can I deep link into the view stack of an app in SwiftUI?

I am attempting to set up deep linking into my app, but I can't figure out how Apple wants us to do this. I have 3 views. FirstView(), SecondView(), and ThirdView(). FirstView has a button on it. When the user taps on that button, I want the app to "deep link" to the ThirdView and then when the user taps on "back" on the ThirdView, I want the app to go to SecondView(). I also want to control this using an EnvironmentObject so I can start this navigation from an arbitrary place. i.e. a push notification or from a web link. Here is my code.

import SwiftUI


@main
struct DeepLinkAppApp: App {
    
    @StateObject var navigationState = NavigationState()
    
    var body: some Scene {
        WindowGroup {
            FirstView().environmentObject(navigationState)
        }
    }
    
    
}


class NavigationState: NSObject, ObservableObject {
    @Published var isOnSecondScreen = false
    @Published var isOnThirdScreen = false
    
}

struct FirstView: View {
    
    @EnvironmentObject var navigationState: NavigationState
    
    var body: some View {
        NavigationView {
            
            VStack {
                Text("First View")
                Button(action: {
                    self.navigationState.isOnSecondScreen = true
                    self.navigationState.isOnThirdScreen = true
                    
                }) {
                    Text("Go to second")
                }
                
                NavigationLink(destination: SecondView(), isActive: $navigationState.isOnSecondScreen) { EmptyView() }
                
                
            }
            
            
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SecondView: View {
    
    @EnvironmentObject var navigationState: NavigationState
    
    var body: some View {
        
        VStack {
            Text("Second View")
            Button(action: {
                self.navigationState.isOnThirdScreen = true
                print("LHere")
                
            }) {
                Text("Go to third")
            }
            
            NavigationLink(destination: ThirdView(), isActive: $navigationState.isOnThirdScreen) {  }
            
            
        }
        
    }
    
    
}

struct ThirdView: View {
    var body: some View {
        
        VStack {
            Text("Third View")
            
            
            
        }
        
    }
}

struct FirstView_Previews: PreviewProvider {
    static var previews: some View {
        FirstView()
    }
}

Upvotes: 2

Views: 2400

Answers (1)

Quang Hà
Quang Hà

Reputation: 4746

There's an example here for deep-linking as your expectation. Please take a look

https://github.com/nalexn/clean-architecture-swiftui/blob/master/CountriesSwiftUI/System/DeepLinksHandler.swift

The trick is delay in seconds for the second view appeared. Later, turn on the switch from second view to third view.

Upvotes: 1

Related Questions