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

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()
}

Leave a Comment