Search code examples
iosswifttimerswiftuiswiftui-navigationlink

How to navigate another view in onReceive timer closure SwiftUI iOS


I am trying to achieve a navigation to another view when timer hits a specific time. For example I want to navigate to another view after 5 minutes. In swift i can easily achieve this but I am new to SwiftUI and little help will be highly appreciated.

My code:

struct TwitterWebView: View {
    
    @State var timerTime : Float
    @State var minute: Float = 0.0
    @State private var showLinkTarget = false
    let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
    
    var body: some View {
        
        WebView(url: "https://twitter.com/")
        .navigationBarTitle("")
        .navigationBarHidden(true)
        
        .onReceive(timer) { _ in
            if self.minute == self.timerTime {
                print("Timer completed navigate to Break view")
                NavigationLink(destination: BreakView()) {
                    Text("Test")
                }
                self.timer.upstream.connect().cancel()
            } else {
                self.minute += 1.0
            }
        }
    }
}

Solution

  • Here is possible approach (of course assuming that TwitterWebView has NavigationView somewhere in parents)

    struct TwitterWebView: View {
    
        @State var timerTime : Float
        @State var minute: Float = 0.0
        @State private var showLinkTarget = false
        let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
    
        @State private var shouldNavigate = false
    
        var body: some View {
    
            WebView(url: "https://twitter.com/")
            .navigationBarTitle("")
            .navigationBarHidden(true)
    
            .background(
                NavigationLink(destination: BreakView(), 
                                  isActive: $shouldNavigate) { EmptyView() }
            )
    
            .onReceive(timer) { _ in
                if self.minute == self.timerTime {
                    print("Timer completed navigate to Break view")
                    self.timer.upstream.connect().cancel()
    
                    self.shouldNavigate = true      // << here
                } else {
                    self.minute += 1.0
                }
            }
        }
    }