Swift Firebase:发送电子邮件后如何检查用户是否经过验证?

问题描述 投票:0回答:1

创建帐户后,我发送带有验证链接的电子邮件:

func sendVerificationMail() {
        if self.authUser != nil && !self.authUser!.isEmailVerified {
            self.authUser!.sendEmailVerification(completion: { (error) in

                // TODO Notify user email was sent or not because of error

            })
        } else {

            // TODO Notify everything is OK

        }
    }

在其他地方,我确认了它:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if Auth.auth().currentUser != nil && Auth.auth().currentUser!.isEmailVerified {
            self.performSegue(withIdentifier: "toMainScreen", sender: self)
        } else {
            self.performSegue(withIdentifier: "notLoggedView", sender: self)
        }
    }

即使确认,我也总是转到notLoggedView。有人可以解释为什么?

ios swift firebase firebase-authentication
1个回答
0
投票

我已经遇到了类似的问题,要解决此问题,我必须重新加载配置文件。试试这个。

func loginUser() {

       Auth.auth().currentUser?.reload(completion: { (error) in

                  if let error = error {
                            print(error)
                  } else {
                          if Auth.auth().currentUser != nil && Auth.auth().currentUser!.isEmailVerified {
                             self.performSegue(withIdentifier: "toMainScreen", sender: self)
                          } else {
                                  self.performSegue(withIdentifier: "notLoggedView", sender: self).  
                               }
                         }
                 })
}

而且,您不必每次都写Auth.auth().currentUser并将其存储在variable中。而且,您可以随时随地使用此功能。

if let authUser = Auth.auth().currentUser { //You can also get  current user like this in a safe way
  //Do your stuff here
}
© www.soinside.com 2019 - 2024. All rights reserved.