How to make if someone writes "admin,"(the password I want) in textfield, and clicks login, it checks if that is correct or not, for further functions based on results
class ViewController: UIViewController {
var name:String?
var login:String?
@objc private func openSecretMenu() {
let alert = UIAlertController(title: "Login", message: "password", preferredStyle: .alert)
// Login button
let loginAction = UIAlertAction(title: "Login", style: .default, handler: { (action) -> Void in
// Get TextFields text
let passwordTxt = alert.textFields![1]
//Asign textfileds text to our global varibles
self.login = passwordTxt.text
print("PASSWORD: (self.login!)")
})
// Cancel button
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })
//2nd textField for password
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Enter password"
textField.isSecureTextEntry = false
}
// Add actions
alert.addAction(cancel)
alert.addAction(loginAction)
self.present(alert, animated: true, completion: nil)
}
}
}
2
Answers
You have a choice to validate user either with server-side or with static content.
}
The problem with UIAlertController is that it always closes if you press one of its buttons. Also the action callback isn’t triggered until after the closing animation (~0.25s), which is annoying too.
So what happens is the user enters a password, presses send, the alert closes, after 0.25s the password gets actually checked. If it is wrong you could just show a new alert, but it’s not ideal.