title: Swift how to safe user login session with checkbox "Remember Me"
body: <p>i have a question about my app that i develop, in my login screen i have this checkbox when the user check that box, his username(or in my case his phone number to login) will automatically safe in the textfield where if the user logout, his phone number will automatically fill in the textfield.</p>
  <p>Where if the user not check that box, his phone number will not gonna safe in the user session so when the user logout, his phone number will not fill the login textfield.</p>
  <p>that checkBox is for &quot;remember me&quot;, when user check that box it will remember the user phone number and if not check the box it will not remember the user phone for the next time the user login.</p>
  <p>any idea how guys?</p>
  <p><a href="https://i.stack.imgur.com/yhsr6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yhsr6.png" alt="Image" /></a></p>
  <p>heres my code :</p>
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      
      rememberMeFlag = UserDefaults.standard.bool(forKey: &quot;REMEMBER_USER&quot;)
      checkBoxBtn.addTarget(self, action: #selector(toggleCheckBox(_:)), for: .valueChanged)
  
      checkBoxBtn.isSelected = rememberMeFlag
  
      if rememberMeFlag {
          let save_session = UserDefaults.standard.string(forKey: &quot;USER_TELEPHONE&quot;)
          textPhoneNumber.text = save_session!.substring(from: 1)
      }
  
      textPhoneNumber.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
  
  }
  
  @IBAction func toggleCheckBox(_ sender: UIButton) {
      
      if sender.isSelected == true {
          checkBoxBtn.setImage(UIImage(named: &quot;Checked-1&quot;), for: .normal)
          sender.isSelected = false
      }else{
          checkBoxBtn.setImage(UIImage(named: &quot;unCheck&quot;), for: .normal)
          sender.isSelected = true
      }
      
  }
  
  
  //this code is for user session
  if FIRST_BOOT {
          print(&quot;first boot&quot;)
          if let save_session = UserDefaults.standard.string(forKey: &quot;USER_FULLNAME&quot;) {
              USER_FULLNAME = save_session
          }
          if let save_session = UserDefaults.standard.string(forKey: &quot;SESSION_TOKEN&quot;) {
              SESSION_TOKEN = save_session
          }
          if let save_session = UserDefaults.standard.string(forKey: &quot;USER_PIN&quot;) {
              USER_PIN = save_session
          }
          if let save_session = UserDefaults.standard.string(forKey: &quot;USER_TELEPHONE&quot;) {
              USER_TELEPHONE = save_session
          }
         
          if let save_session = UserDefaults.standard.string(forKey: &quot;IDENTITY_NUMBER&quot;) {
              IDENTITY_NUMBER = save_session
          }
          
          if let _ = UserDefaults.standard.string(forKey: &quot;WIZARD&quot;) {
              // Wizard has opened
          } else {
              //performSegue(withIdentifier: &quot;loginToWizard&quot;, sender: nil)
              
              //return
          }
  
  
  
    //i use that code to put the user phone num into the textfield
  if let save_session = UserDefaults.standard.string(forKey: &quot;USER_TELEPHONE_CACHED&quot;) {
          textPhoneNumber.text = save_session.substring(from: 1)
      }
    
    
  </code></pre>
  <p>can anyone tell me how to do the thing that i want? i want just to put that user session inside the checkbox func but i just see alot of bugs after that, im asking about the steps on how to do it, cause im kinda confused how even though its seems not so difficult.... thanks guys.</p>