How I can implement Login / Logout Navigation using UserDefaults in swift?

**strong text**Every time when I run the app I have to do the login to enter the home page of the app.

How can I store the session when I tap the login button untill I Tap the logout button.

So, that I can avoid the login everytime when I run my application.

Please help me out......

Other solutions are not working.

class LoginViewController: UIViewController {

    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var loginName: UITextField!

   @IBAction func submitButton(_ sender: Any) {

  UserDefaults.standard.set(true, forKey: "isLoggedIn")

        if loginName.text == "test" && password.text == "test" {

            UserDefaults.standard.set(true, forKey: "status")
            Switcher.updateRootVC()

        }
        else{
            print("Invalid credentials")
        }
    }
}

class ProfileViewController: UIViewController {

@IBAction func submitButton(_ sender: Any) {

    UserDefaults.standard.set(false, forKey: "status")
    Switcher.updateRootVC()

    }

}
class AppDelegate: UIResponder, UIApplicationDelegate {


  var window: UIWindow?

    func loadBaseController() {
       let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
       guard let window = self.window else { return }
       window.makeKeyAndVisible()
       if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
           let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
           self.window?.rootViewController = loginVC
       } else {
           let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
           let navigationHomeVC = UINavigationController(rootViewController: homeVC)
           self.window?.rootViewController = navigationHomeVC
       }
      self.window?.makeKeyAndVisible()
    }


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

       self.loadBaseController()

        return true

    }
}

I referred the same method of this link https://medium.com/@paul.allies/ios-swift4-login-logout-branching-4cdbc1f51e2c but it is not useful because I'm getting not the expected result .
I'm the correct status code but not the correct view controller. Please help me out to solve this problem........

In the given link it is adding the TabbarVc to the tabor


In Xcode 11 there are 2 file while we create a new project so if you want to change rootViewController from the delegate file then you need to load that controller from the SceneDelegate.swift file.

//SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene

    self.loadBaseController()
}


func loadBaseController() {
   let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
   guard let window = self.window else { return }
   window.makeKeyAndVisible()
   if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
       let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
       self.window?.rootViewController = loginVC
   } else {
       let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
       let navigationHomeVC = UINavigationController(rootViewController: homeVC)
       self.window?.rootViewController = navigationHomeVC
   }
    self.window?.makeKeyAndVisible()
}

You Have to use SceneDelegate to solve this as said in previous comment. But if you still want to use Switcher Class to do this. Here is the solution: Add-> private(set) static var shared: SceneDelegate? and Self.shared = self Switcher.updateRootVC()

after adding those lines. now edit the Switcher class like this:

Modify the switcher class like this.

now you can Switch from anywhere using switcher class..