Home > Software engineering >  How do I make a correct password, for popover (UIAlertController) login?
How do I make a correct password, for popover (UIAlertController) login?

Time:08-13

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)
        
    }


}
}

CodePudding user response:

You have a choice to validate user either with server-side or with static content.

class ViewController: UIViewController {
var name:String?
var login:String?

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton()
    button.frame = CGRect(x: 32, y: self.view.frame.midY, width: self.view.frame.width - 64, height: 60)
    button.addTarget(self, action: #selector(openSecretMenu), for: .touchUpInside)
    button.setTitle("Open Popup", for: .normal)
    button.backgroundColor = .green
    button.titleLabel?.font = .boldSystemFont(ofSize: 21)
    self.view.addSubview(button)
}

@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![0]
        //Asign textfileds text to our global varibles
        self.login = passwordTxt.text
        
        // api call for login with fetched password, then server will process
        // if login success then you can move forward (push view controller) from here
        
        // for static case
        print("PASSWORD: \(self.login!)")
        if self.login == "admin" {
            print("succeed")
        } else {
            print("password not matched")
        }
    })
    // 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)
}

}

CodePudding user response:

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.

  • Related