0

我使用 UserDefaults 来制作登录屏幕。我的问题是我需要一个管理员让苹果的审查员可以直接登录我的应用程序而无需再次注册。接下来我该怎么办?请帮助我!谢谢!

这是我的代码:

//This is register's code
 @IBOutlet weak var userRegisterTextField: UITextField!

@IBOutlet weak var userRegisterPasswordTextField: UITextField!

@IBOutlet weak var repeatPasswordTextField: UITextField!

@IBAction func registerButton(_ sender: Any) {


    let userRegister = userRegisterTextField.text
    let userRegisterPassword = userRegisterPasswordTextField.text
    let userRepeatPassword = repeatPasswordTextField.text

    // save the data that user register in.
    UserDefaults.standard.set(userRegister, forKey: "userRegister")
    UserDefaults.standard.set(userRegisterPassword, forKey: "userRegisterPassword")
    UserDefaults.standard.synchronize()

    }




//This is login's code.
@IBOutlet weak var userName: UITextField!

@IBOutlet weak var userPassword: UITextField!

@IBOutlet weak var loginText: UILabel!

@IBOutlet weak var loginButton: UIButton!

@IBAction func loginButton(_ sender: Any) {

    let userLogin = userName.text
    let userPasswordText = userPassword.text
    let userPasswordStored = UserDefaults.standard.string(forKey: "userRegisterPassword")
    let userLoginStored = UserDefaults.standard.string(forKey: "userLoginStored")
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController





    /*userID and password must the same as the data that user registered before*/

    if userLoginStored == userLogin {
    if userPasswordStored == userPasswordText {

            UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
            UserDefaults.standard.synchronize()
            self.present(vc, animated: true, completion: nil)   

        }

    } else {
        loginText.text = "Your userID or password is wrong. Please try again"
    }

}
4

1 回答 1

0

您正在为用户名使用两个不同的密钥。检查您的代码以获取以下行

在注册时,您使用密钥“userRegister”来存储用户名。

UserDefaults.standard.set(userRegister, forKey: "userRegister")

在登录时,您使用密钥“userLoginStored”来检索密码。

let userLoginStored = UserDefaults.standard.string(forKey: "userLoginStored")

在这两个地方都使用相同的密钥“userRegister”,这样您的问题就会得到解决。

于 2017-03-28T10:24:34.927 回答