gpt4 book ai didi

ios - 即使登录错误也能登录

转载 作者:可可西里 更新时间:2023-11-01 01:23:00 27 4
gpt4 key购买 nike

我正在使用 Firebase 进行登录/注册身份验证,但遇到了问题。我已完成所有设置并且工作正常,但我在登录部分遇到了一些问题。

这是我的代码:

@IBAction func clickLogin(_ sender: UIButton) {

FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

if let error = error {
print(error.localizedDescription)
}
})

performSegue(withIdentifier: "toMainSegue", sender: self) //Issue
}

错误的是,当电子邮件或密码不正确时,它仍然会执行 segue。我试过:

@IBAction func clickLogin(_ sender: UIButton) {

FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

if let error = error {
print(error.localizedDescription)
} else {
performSegue(withIdentifier: "toMainSegue", sender: self) //Error Line
}
})

但是我得到一个错误:

Implicit use of ‘self’ in closure, use ‘self.’ to capture semantics explicit.

当且仅当登录成功时,是否有更好的方法将用户带到下一个 UI?

最佳答案

在您分享的代码中

    @IBAction func clickLogin(_ sender: UIButton) {

FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

if let error = error {
print(error.localizedDescription)
}
})

performSegue(withIdentifier: "toMainSegue", sender: self) //Issue
}

performSegue(withIdentifier:sender:) 方法在@IBAction 中调用,而不是在 signIn(withEmail:password:completion) 方法的完成处理程序中调用。因此,无论后者编写或执行了什么,您的 performSegue(withIdentifier:sender:) 都会被调用。尝试修改代码如下

    @IBAction func clickLogin(_ sender: UIButton) {

FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

if let error = error {
print(error.localizedDescription)
} else {
self.performSegue(withIdentifier: "toMainSegue", sender: self)
}
})
}

请记住,因为逻辑是在闭包中执行的,所以您需要在方法和变量之前指定 self. 前缀!

关于ios - 即使登录错误也能登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com