title: how to keep a user login even if they close the app? I'm using swift 3, and Firebase
body: <p>I'm new to coding and to Stack overflow, I'm trying to have a user stay logged in even after they close the app. I also don't want them to always see the login in screen. how do I do i go about keeping the user Login even if they close the app and re-open the app. I'm using Swift 3.0, Xcode 8, and Firebase. </p>
  
  <pre><code>import UIKit
  import Firebase
  import SwiftKeychainWrapper
  
  
  class LoginViewController: UIViewController {
  
      @IBOutlet weak var emailField: UITextField!
      @IBOutlet weak var pwField: UITextField!
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
          //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
          //tap.cancelsTouchesInView = false
  
          view.addGestureRecognizer(tap)
  
          // Do any additional setup after loading the view.
      }
  
  
      func dismissKeyboard() {
          //Causes the view (or one of its embedded text fields) to resign the first responder status.
          view.endEditing(true)
      }
  
      @IBAction func loginPressed(_ sender: Any) {
          guard emailField.text != "", pwField.text != "" else {return}
  
          FIRAuth.auth()?.signIn(withEmail: emailField.text!, password: pwField.text!, completion: { (user, error) in
              if let error = error {
                  print(error.localizedDescription)
              }
  
              if user != nil {
                  let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBarViewController")
  
                  self.present(vc, animated: true, completion: nil)
              }
          })
      }
  }
  </code></pre>
  
  <p><strong>Below is my UsersViewController code it has the log-out button</strong></p>
  
  <pre><code>import UIKit
  </code></pre>
  
  <p>import Firebase</p>
  
  <p>class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {</p>
  
  <pre><code>@IBOutlet weak var tableview: UITableView!
  
  var user = [User]()
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  
      retrieveUsers()
  }
  
  
  func retrieveUsers() {
      let ref = FIRDatabase.database().reference()
  
      ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
  
          let users = snapshot.value as! [String : AnyObject]
          self.user.removeAll()
          for (_, value) in users {
              if let uid = value["uid"] as? String {
                  if uid != FIRAuth.auth()!.currentUser!.uid {
                      let userToShow = User()
                      if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String {
                          userToShow.fullName = fullName
                          userToShow.imagePath = imagePath
                          userToShow.userID = uid
                          self.user.append(userToShow)
                      }
                  }
              }
          }
          self.tableview.reloadData()
      })
      ref.removeAllObservers()
  
  }
  
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableview.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell
  
      cell.nameLabel.text = self.user[indexPath.row].fullName
      cell.userID = self.user[indexPath.row].userID
      cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
      checkFollowing(indexPath: indexPath)
  
      return cell
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return user.count 
  }
  
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
      let uid = FIRAuth.auth()!.currentUser!.uid
      let ref = FIRDatabase.database().reference()
      let key = ref.child("users").childByAutoId().key
  
      var isFollower = false
  
      ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
  
          if let following = snapshot.value as? [String : AnyObject] {
              for (ke, value) in following {
                  if value as! String == self.user[indexPath.row].userID {
                      isFollower = true
  
                      ref.child("users").child(uid).child("following/\(ke)").removeValue()
                      ref.child("users").child(self.user[indexPath.row].userID).child("followers/\(ke)").removeValue()
  
                      self.tableview.cellForRow(at: indexPath)?.accessoryType = .none
                  }
              }
          }
          if !isFollower {
              let following = ["following/\(key)" : self.user[indexPath.row].userID]
              let followers = ["followers/\(key)" : uid]
  
              ref.child("users").child(uid).updateChildValues(following as Any as! [AnyHashable : Any])
              ref.child("users").child(self.user[indexPath.row].userID).updateChildValues(followers)
  
              self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
          }
      })
      ref.removeAllObservers()
  
  }
  
  
  func checkFollowing(indexPath: IndexPath) {
      let uid = FIRAuth.auth()!.currentUser!.uid
      let ref = FIRDatabase.database().reference()
  
      ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
  
          if let following = snapshot.value as? [String : AnyObject] {
              for (_, value) in following {
                  if value as! String == self.user[indexPath.row].userID {
                      self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
                  }
              }
          }
      })
      ref.removeAllObservers()
  
  }
  
  
  
  
  
  
  @IBAction func logOutPressed(_ sender: Any) {
  
  
      do {
  
          try FIRAuth.auth()?.signOut()
  
          if FIRAuth.auth()?.currentUser == nil {
  
              // Remove User Session from device
              UserDefaults.standard.removeObject(forKey: "uid")
              UserDefaults.standard.synchronize()
  
              let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController
  
          }
  
      } catch let signOutError as NSError {
  
          // handle logout error
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>extension UIImageView {</p>
  
  <pre><code>func downloadImage(from imgURL: String!) {
      let url = URLRequest(url: URL(string: imgURL)!)
  
      let task = URLSession.shared.dataTask(with: url) {
          (data, response, error) in
  
          if error != nil {
              print(error!)
              return
          }
  
          DispatchQueue.main.async {
              self.image = UIImage(data: data!)
          }
  
      }
  
      task.resume()
  }
  </code></pre>
  
  <p>}</p>