我正在为我的应用使用Firebase进行电子邮件+密码身份验证。登录有效,我使用observeAuthEventWithBlock
检查用户是否已登录 - 以便不显示登录页面。如果我按下主页按钮并再次打开应用程序,则没有问题。我遇到的问题是如果我强行退出应用程序。当我重新打开时,我必须再次登录。
在我显示登录代码之前有关设置的一些注意事项。
登录代码:
@IBAction func loginButtonPressed() {
let userEmail = emailTextField.text
self.ref.authUser(self.emailTextField.text, password: self.passwordTextField.text, withCompletionBlock: { (error, auth) -> Void in
guard error == nil else {
if let errorCode = FAuthenticationError(rawValue: error.code) {
switch (errorCode) {
case .EmailTaken:
self.displayMessage("Email Error", theMessage: "This email is taken")
case .InvalidEmail:
self.displayMessage("Email Error", theMessage: "This email is invalid")
case .UserDoesNotExist:
self.displayMessage("User Error", theMessage: "A user account for email: \(userEmail!) does not exist")
case .InvalidPassword:
self.displayMessage("Password Error", theMessage: "The password is incorrect")
case .NetworkError:
self.displayMessage("Network Error", theMessage: "Seems like there's a problem with your internet connection")
default:
return
}
}
return //set Unknown Error Alert here
}
print("LOGGED IN: segue from loginButtonPressed")
self.userLoggedIn = true
print("user is logged in? \(self.userLoggedIn)")
self.performSegueWithIdentifier("loginLocaleSegue", sender: self)
})
}
检查用户是否已登录 - 如果是这样,请执行navcon,弹出并显示嵌入式View Controller:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if self.userLoggedIn.boolValue == true {
ref.observeAuthEventWithBlock { (authData) -> Void in
if authData != nil {
let navCon: UINavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainNavigationController") as! UINavigationController
self.presentViewController(navCon, animated: false, completion: nil)
navCon.popViewControllerAnimated(false)
print("user is authenticated: \(authData.providerData["email"] as! String)")
print("segues from viewDidAppear")
} else {
return
}
}
}
}
我见过与Firebase auth相关的问题,其中默认情况下Authdata存储在Keychain中,这导致即使在删除app后Authdata仍然存在问题,但我遇到了完全相反的问题。有什么想法吗?
答案 0 :(得分:0)
据我所知,你并没有将“self.userLoggedIn = true”写入任何数据库,因此当你让应用程序处于空闲状态时它仍然是“真实”是有意义的,但是一旦你关闭了应用程序然后它变为零(没有价值),这是因为它在应用程序打开时只在后台冷却,但没有完全关闭。尝试将此内容写入您的Firebase数据库,如本教程中所述,并查看是否有帮助。
https://www.raywenderlich.com/109706/firebase-tutorial-getting-started