Hi,
In our current swiftUI app, if user closes the app they will be asked to login again - we are trying to change this behaviour by using userDefaults.
Without userDefaults code changes below LoginView directs to the home screen after login / registration using the Navigation link.
Issue :
We made below code changes to the View and LoginViewModel and when login / register button is pressed the LoginView is not taking to the home screen. It just shows the login/signup page with the login successful or registration successful message. I have debugged in Xcode and loginOrSignUpSuccessful variable value is set to true after login button press.
Why is navigationlink not directing to MainLandingTabView Screen after login or registration ?
Code Changes made to the LoginViewModel
Variable : @Published var loginOrSignUpSuccessful = UserDefaults.standard.bool(forKey: "loginOrSignUpSuccessful"){
didSet{
UserDefaults.standard.set(self.loginOrSignUpSuccessful, forKey:"loginOrSignUpSuccessful")
}
}
AuthenticateUser Function code snippet :
self.loginOrSignUpSuccessful = true UserDefaults.standard.set(true, forKey: "loginOrSignUpSuccessful")
SignUpLogin View Code Snippet :
struct SignUpLogin: View {
private var showMainLandingTabViewLink: some View{
NavigationLink(destination: MainLandingTabView().environmentObject(signupLoginViewModelStateObject).navigationBarBackButtonHidden(true), isActive: $signupLoginViewModelStateObject.loginOrSignUpSuccessful, label: {EmptyView()}) }
var body: some View { NavigationView{ ZStack(alignment: .leading) { // all the email & password, button fields. } .background(showMainLandingTabViewLink) }.environmentObject(signupLoginViewModelStateObject) .navigationBarHidden(true) // Used to hide the back buttom if coming from NavigationView }