Swift how to safe user login session with checkbox "Remember Me"

2.4k Views Asked by At

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.

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.

that checkBox is for "remember me", 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.

any idea how guys?

Image

heres my code :

override func viewDidLoad() {
    super.viewDidLoad()
    
    rememberMeFlag = UserDefaults.standard.bool(forKey: "REMEMBER_USER")
    checkBoxBtn.addTarget(self, action: #selector(toggleCheckBox(_:)), for: .valueChanged)

    checkBoxBtn.isSelected = rememberMeFlag

    if rememberMeFlag {
        let save_session = UserDefaults.standard.string(forKey: "USER_TELEPHONE")
        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: "Checked-1"), for: .normal)
        sender.isSelected = false
    }else{
        checkBoxBtn.setImage(UIImage(named: "unCheck"), for: .normal)
        sender.isSelected = true
    }
    
}


//this code is for user session
if FIRST_BOOT {
        print("first boot")
        if let save_session = UserDefaults.standard.string(forKey: "USER_FULLNAME") {
            USER_FULLNAME = save_session
        }
        if let save_session = UserDefaults.standard.string(forKey: "SESSION_TOKEN") {
            SESSION_TOKEN = save_session
        }
        if let save_session = UserDefaults.standard.string(forKey: "USER_PIN") {
            USER_PIN = save_session
        }
        if let save_session = UserDefaults.standard.string(forKey: "USER_TELEPHONE") {
            USER_TELEPHONE = save_session
        }
       
        if let save_session = UserDefaults.standard.string(forKey: "IDENTITY_NUMBER") {
            IDENTITY_NUMBER = save_session
        }
        
        if let _ = UserDefaults.standard.string(forKey: "WIZARD") {
            // Wizard has opened
        } else {
            //performSegue(withIdentifier: "loginToWizard", sender: nil)
            
            //return
        }



  //i use that code to put the user phone num into the textfield
if let save_session = UserDefaults.standard.string(forKey: "USER_TELEPHONE_CACHED") {
        textPhoneNumber.text = save_session.substring(from: 1)
    }
  
  

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.

1

There are 1 best solutions below

6
On BEST ANSWER

I suggest the following:

  • when the user checks or unchecks "remember me", update an instance variable rememberMeFlag and store this information in the user defaults
  • when the user edits the telephone text field and rememberMeFlag is true, save the contents to the user defaults
  • when the view loads, read the values from the user defaults and update rememberMeFlag, the checkbox and the text field

So the code might look like this:

class X: UIViewController {
    
    private var rememberMeFlag = false
    @IBOutlet weak var rememberMeSwitch:UISwitch!
    @IBOutlet weak var telephoneTextField:UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        rememberMeFlag = UserDefaults.standard.bool(forKey: "REMEMBER_USER")
        rememberMeSwitch.isOn = rememberMeFlag
        
        if rememberMeFlag {
            let tel = UserDefaults.standard.string(forKey: "USER_TELEPHONE")
            telephoneTextField.text = tel
        }
        
        // connect actions; do this in code like here
        // or use outlet connections from the storyboard
        telephoneTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        rememberMeSwitch.addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged)
    }
    
    @IBAction func toggleCheckBox(_ sender: UIButton) {
        rememberMeFlag = !rememberMeFlag
        UserDefaults.standard.set(rememberMeFlag, forKey: "REMEMBER_USER")

        if rememberMeFlag == true {
            checkBoxBtn.setImage(UIImage(named: "Checked-1"), for: .normal)
            let text = telephoneTextField.text
            UserDefaults.standard.set(text, forKey:"USER_TELEPHONE")
        } else {
            checkBoxBtn.setImage(UIImage(named: "unCheck"), for: .normal)
            UserDefaults.standard.removeObject(forKey: "USER_TELEPHONE")
        }   
    }

    @objc func textFieldDidChange(_ sender: UITextField){
        guard rememberMeFlag else { return }
        let text = telephoneTextField.text
        UserDefaults.standard.set(text, forKey:"USER_TELEPHONE")
    }
}