title: UserDefaults Auto-Login trouble
body: <p>I have tried everything under the sun to allow the user to stay logged in via UserDefaults. However, every time I log in, close out of the app, and open it again, the app reverts to the login screen. I have tried using the solutions from other similar questions, but none have worked.</p>
  <p>Below is some code from my login screen when the &quot;Log In&quot; button is tapped:</p>
  <pre><code>@IBAction func logInTapped(_ sender: Any) {
      let email = firstNameTF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
      let password = lastNameTF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  
      
      Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
          if error != nil{
              //couldnt sign in
              self.errorLabel.text = error!.localizedDescription
              self.errorLabel.alpha = 1
          } else {
              if #available(iOS 13.0, *) {
                  self.errorLabel.text = &quot;Logged in&quot;
                  self.errorLabel.isHidden = false
                  print(&quot;log in successful&quot;)
                  self.userDefaults.setValue(true, forKey: &quot;user_uid_key&quot;)
                  self.userDefaults.synchronize()
                  self.transitionToHome()
  
              } else {
              }
          }
      }
             
  }
  </code></pre>
  <p>Finally, here is the code from my AppDelegate:</p>
  <pre><code>var window: UIWindow?
  let userDefaults = UserDefaults.standard
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
  
  
      
      FirebaseApp.configure()
          check()
      window = UIWindow()
      window?.makeKeyAndVisible()
  </code></pre>
  <p>//        window?.rootViewController = UINavigationController(rootViewController: SViewController())</p>
  <pre><code>    return true
  }
      func check() {
          if userDefaults.value(forKey: &quot;user_uid_key&quot;) as? Bool == true {
  
              
              self.window = UIWindow(frame: UIScreen.main.bounds)
  </code></pre>
  <p>Link to zip file for debug project: <a href="https://drive.google.com/file/d/1Hd-E4yaHN70nH2wj0l9rP6xDLUC9oHEA/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1Hd-E4yaHN70nH2wj0l9rP6xDLUC9oHEA/view?usp=sharing</a></p>
  <p>New SceneDelgeate:</p>
  <pre><code> func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      
      guard let windowScene = (scene as? UIWindowScene) else { return }
      
      if userDefaults.value(forKey: &quot;user_uid_key&quot;) as? Bool == true {
          print(&quot;userdefaults function is running&quot;)
          
  
          self.window = UIWindow(windowScene: windowScene)
    //                        self.window = UIWindow(windowScene: UIScreen.main.bounds)
  
          let storyboard = UIStoryboard(name: &quot;Main&quot;, bundle: Bundle.main)
          
          let initialViewController = storyboard.instantiateViewController(withIdentifier: &quot;TBCID&quot;) as! UITabBarController
  
          self.window?.rootViewController = initialViewController
  
          self.window?.makeKeyAndVisible()
  
  
      }
  }
  </code></pre>