Search code examples
iosswiftswiftuiswiftui-navigationlinkswiftui-navigationview

SwiftUI Navigation link deactivated after switching apps


I am building an EV Station app on IOS, which provides users with a map and pins representing EV Charging Stations. I have a NavigationLink with destination: another view that applies filters to the map results. And label: A custom built button. The problem is that when I switch apps and returns again to my app the NavigationLink is not working anymore, not until I switch apps again and return to my app a second time, only then the NavigationLink works again.

This behavior is repeated when I repeat the steps described above.

Below you can see my MainView which contains the above mentioned NavigationLink. you can also find and clone the whole project on github.

import SwiftUI
import MapKit
import CoreLocationUI
import Foundation
import AlertToast

struct MainView: View {    
    @StateObject var locationController = LocationController()
    @StateObject var networkController = NetworkController()
    
    @State public var showingSheet = false
    @State public var dismissSheet = false
    
    @State private var showToast = false
    
    @State public var applyFilters = false
    
    @State public var type1 = false
    @State public var type2 = false
    @State public var csstype1 = false
    @State public var csstype2 = false
    @State public var chademo = false
    @State public var schuko = false
    @State public var tesla = false
    
    @State public var dc = false
    @State public var ac1 = false
    @State public var ac2 = false
    @State public var ac2s = false
    @State public var ac3 = false
    
    @State public var format: Int = 3
    @State public var status: Int = 7
    
    var map: MapView {
        MapView(
            locationController: locationController,
            networkController: networkController,
            showingSheet: $showingSheet,
            dismissSheet: $dismissSheet,
            didSelect: {_ in}
        )
        
    }
    
    var body: some View {
        NavigationView {
            ZStack(alignment: .bottom) {
                map.edgesIgnoringSafeArea(.bottom)
                
                LocationButton(.currentLocation) {
                    print("tapped")
                    locationController.requestAllowOnceLocationPermission()
                }
                .foregroundColor(.white)
                .cornerRadius(8)
                .labelStyle(.iconOnly)
                .tint(Color("LightBlue"))
                .padding(.bottom, 100 )
                .padding(.leading, 330 )
                
                NavigationLink(
                    destination: FiltersView(
                        applyFilters: $applyFilters,  
                        type1: $type1, 
                        type2: $type2,  
                        csstype1: $csstype1, 
                        csstype2: $csstype2, 
                        chademo: $chademo, 
                        schuko: $schuko, 
                        tesla: $tesla, 
                        dc: $dc, 
                        ac1: $ac1, 
                        ac2: $ac2, 
                        ac2s: $ac2s, 
                        ac3: $ac3, 
                        format: $format, 
                        status: $status),
                    label: {
                        ZStack {
                            RoundedRectangle(cornerRadius: 8, style: .circular)
                                .fill(Color("LightBlue"))
                            HStack {
                                SwiftUI.Image("Filters")
                                    .resizable()
                                    .scaledToFit()
                                .padding(10)
                                Text("Φίλτρα")
                                    .foregroundColor(.white)
                                    .font(.system(size: 15, weight: .regular))
                                    .padding(.trailing, 5)
                            }
                        }.frame(minWidth: 100, maxWidth: 110, minHeight: 40, maxHeight: 40)
                    })
                    .padding(.bottom, 660 )
                    .padding(.leading, 250 )
            }
            //.preferredColorScheme(.dark)
            .background(Color("DeepBlue"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    SwiftUI.Image("Logo")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 100, height: 100)
                    
                }
                
            }
            .sheet(item: $networkController.activeLocation, onDismiss: onSheetDismiss) { item in
                BottomSheetView(data: item)
                    .onDisappear() {
                        print("dissapear")
                    }
            }
            .onAppear {
                print("Active View: Main")
                if applyFilters {
                    networkController.applyFilters = .constant(true)
                    NetworkVariables.FilterOptions.type1 = type1
                    NetworkVariables.FilterOptions.type2 = type2
                    NetworkVariables.FilterOptions.csstype1 = csstype1
                    NetworkVariables.FilterOptions.csstype2 = csstype2
                    NetworkVariables.FilterOptions.chademo = chademo
                    NetworkVariables.FilterOptions.schuko = schuko
                    NetworkVariables.FilterOptions.tesla = tesla
                    NetworkVariables.FilterOptions.dc = dc
                    NetworkVariables.FilterOptions.ac1 = ac1
                    NetworkVariables.FilterOptions.ac2 = ac2
                    NetworkVariables.FilterOptions.ac2s = ac2s
                    NetworkVariables.FilterOptions.ac3 = ac3
                    NetworkVariables.FilterOptions.format = format
                    NetworkVariables.FilterOptions.status = status
                    Task {
                        await networkController.getToken()
                        showToast.toggle()
                    }
                    applyFilters = false
                }
            }
        }
        .toast(isPresenting: $showToast){
            AlertToast(type: .regular, title: "Βρέθηκαν \(networkController.mapLocations.count) σημεία")
        }
        
    }
    
    func onSheetDismiss() {
        print("sheet dismissed")
        map.dismissSheet = true
    }
}

Solution

  • I found what the problem was. As I had read before, this kind of problem usually happens because the app is running on an emulator. So while I was having this problem on physical devices, I realised that the physical devices I was testing were running the app on Testflight which is kind of a sandbox as far as I've understood it. So, the problem was caused because the app was running on testflight, when I installed the app directly to an iOS device through USB, it worked just fine.

    Thanks for your replies.