0

I used UserDefaults to make a login Screen. My question is I need an administrator let Apple's examiner can direct sign in my app without register again. What should I do next? Please help me!Thanks!

Here is my code:

//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"
    }

}
SU William
  • 21
  • 5

1 Answers1

0

You are using two different key for username. Check your code for below line

In register you are using key "userRegister" to store username.

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

where in login you are using key "userLoginStored" to retrieve password.

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

At the both places use same key "userRegister" so you will get your problem resolved.

Abhijit
  • 173
  • 1
  • 7
  • To Abhijit: THANK YOU SO MUCH!!!!! It's work!! To anyone see this commit: I added this code: if (userLogin == "456") || (userPasswordText == "789") { self.present(vc, animated: true, completion: nil) } You will get an administrator ID is 123 and password is 789 . Thank's you again!! – SU William Mar 28 '17 at 11:34