skip login view Controller if you are already authorized? swift 4


You can use UserDefaults, in viewController


UserDefaults.standard.set(loginTextField.text, forKey: "login")

 @IBAction func login(_ sender: Any) {
        UserDefaults.standard.set(loginTextField.text, forKey: "login")
          let vc = HomeViewController()
            navigationController?.pushViewController(vc, animated: true)
}

I do this in my AppDelegate, my initial view controller assumes that my user is logged in. Thus, in my AppDelegate I do the following to decide whether I need to show the login screen:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        var route = UIViewController()
        if UserDefaults.standard.string(forKey: "token") != nil {
            route = HomeViewController()
        } else {
            route = OnBoardingViewController()
        }
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: route)
        
        
        return true
    }

No comments: