i have scenedelegate, appdelegate, signinVC, signupVC, and homeVC
viewcontroller flow like below
UINavigationController(initialVC) ->SigninVC (signin and signup buttons) -> homeVC(logoutBtn)
i need when i signIn or signUp i need to move homeVC and if i logout i need back to loginVC and i need user auto login until i hit logout button in homeVC
in signupVC:
class RegisterViewController: UIViewController {
@IBOutlet weak var firstNameTextfield: UITextField!
@IBOutlet weak var lastNameTextfield: UITextField!
@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!
@IBAction func registerBtnClicked(_ sender: Any) {
if firstNameTextfield.text?.isEmpty == true{
self.showAlert(title: "Registration", message: "please enter first name")
}
else if lastNameTextfield.text?.isEmpty == true{
self.showAlert(title: "Registration", message: "please enter last name")
}else if emailTextfield.text?.isEmpty == true{
self.showAlert(title: "Registration", message: "please enter email")
}else if passwordTextfield.text?.isEmpty == true{
self.showAlert(title: "Registration", message: "please enter password")
}
else{
UserDefaults.standard.set(firstNameTextfield.text, forKey: "userName")
UserDefaults.standard.set(lastNameTextfield.text, forKey: "lastName")
UserDefaults.standard.set(emailTextfield.text, forKey: "email")
UserDefaults.standard.set(passwordTextfield.text, forKey: "userPassword")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
navigationController?.pushViewController(vc, animated: true)
}
}
in signinVC:
class ViewController: UIViewController {
@IBOutlet weak var userNameTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!
@IBAction func loginBtnClicked(_ sender: Any) {
let uName = UserDefaults.standard.string(forKey: "userName")
let uPassword = UserDefaults.standard.string(forKey: "userPassword")
if userNameTextfield.text == uName && passwordTextfield.text == uPassword{
UserDefaults.standard.set(true, forKey: "USER_LOGIN")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
navigationController?.pushViewController(vc, animated: true)
}
else{
showAlert(title: "LogIn", message: "please enter username and password")
}
}
@IBAction func registerBtnClicked(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
navigationController?.pushViewController(vc, animated: true)
}
}
in homeVC:
class HomeViewController: UIViewController {
@IBAction func logoutBtnClicked(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "USER_LOGIN") //logging session off
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
navigationController?.pushViewController(vc, animated: true)
}
}
with the above code only recent registered user able to login and auto login not working
how to login all registered users and how to auto login to home page until i hit logout button.. please do help with code
In signinVC add the following line in viewDidLoad
Modify signupVC like following
Finally modify loginBtnClicked method as following