使用UserDefaults存储用户名和密码

时间:2019-03-29 13:10:39

标签: swift nsuserdefaults

我想将用户名,密码和芯片号保留在应用程序的内存中。它现在将其保存在内存中,但是当我从后台打开和关闭应用程序时,数据会丢失。但是,如果我在loginview页面上关闭该应用程序,则在MainView页面上关闭该应用程序时,数据将丢失。这意味着,如果我关闭了除loginview页面之外的其他页面上的应用程序,则数据将被删除。我该怎么办?

我可以使用Labelcheckbox记住或回忆用户的数据。

override func viewDidLoad() {
        super.viewDidLoad()
        chipField.text = chipInfoString
        let save = UserDefaults.standard.bool(forKey: "RememberMe")
        self.circleBox.isChecked = save
        self.labelcheckbox.text = UserDefaults.standard.string(forKey: "RememberMe1")
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(setdata) , userInfo: nil, repeats: true)
        rememberaction()
     }
 let mainTableVC = MainTableViewController()
                    mainTableVC.chip1InfoString = self.chipField.text
                    mainTableVC.emailString = self.emailField.text
                    mainTableVC.passwordString = self.passwordField.text

let navController = UINavigationController(rootViewController: mainTableVC)

                    self.present(navController, animated: true, completion: {

                    })

 @objc func rememberaction(){
        if labelcheckbox.text == "Remember" {
            rememberme()

        }
        if labelcheckbox.text == "Don't Remember" {
            deleteee((Any).self)

        }

    }
 @objc func rememberme() {
        let mail = UserDefaults.standard.string(forKey: "mail")
        let password = UserDefaults.standard.string(forKey: "password")


        if let x = mail {
            self.emailField.text = x
        }


        if let y = password {
            self.passwordField.text = y
        }

        let save3 = UserDefaults.standard.object(forKey: "chipnumber")

        if let z = save3 as? String {
            self.chipField.text = z
        }

    }
@objc func setdata()
    {
        if labelcheckbox.text == "Remember" {
    UserDefaults.standard.set(true, forKey:"RememberMe");
    UserDefaults.standard.set(labelcheckbox.text, forKey: "RememberMe1")
    UserDefaults.standard.set(emailField.text, forKey: "mail")
    UserDefaults.standard.set(passwordField.text, forKey: "password")
    UserDefaults.standard.set(chipField.text, forKey: "chipnumber")
            UserDefaults.standard.synchronize()

        }
else{
            if labelcheckbox.text == "Don't remember"{
    UserDefaults.standard.set(false, forKey:"RememberMe");
    UserDefaults.standard.set(labelcheckbox.text, forKey: "RememberMe1")
    UserDefaults.standard.set(chipField.text, forKey: "chipnumber")
}
}
    }


class MainTableViewController: UITableViewController, UITabBarControllerDelegate{
@objc func action(_ sender: Any){
        UserDefaults.standard.set(chip1InfoString, forKey: "chipnumber")
        UserDefaults.standard.set(emailString, forKey: "email")
        UserDefaults.standard.set(passwordString, forKey: "password")
        UserDefaults.standard.synchronize()


    }
override func viewDidLoad() {
        super.viewDidLoad()

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {

            self.action((Any).self)
        })

0 个答案:

没有答案