我在Xcode模拟器中运行应用程序并使用用户和密码登录然后我退出,logOutOutlet
和bookingsOutlet
被隐藏,logInOutlet
按钮显示。到目前为止,它的行为符合预期。
接下来,从Xcode停止应用程序并再次运行它,但现在它的行为就好像我已经登录:显示logOutOutlet
,bookingsOutlet,隐藏logInOutlet。当应用程序被暂停,终止或删除时,它的行为相同。 (即,在Xcode或cmd + H * 2中停止并向上滑动。)
现在,假设我在下一个ViewController中有一个活动的logIn会话,我尝试从FireBase实时数据库中检索数据,但是我收到了这个错误。 Error Domain=com.firebase Code=1 "Permission Denied" UserInfo={NSLocalizedDescription=Permission Denied}. Why can I not log out completely?
@IBAction func logOut(sender: AnyObject) {
// if the current user is logged in, try to log out and if logout is successful:
// hide: logOut button & bookings button
// show: logIn button
if FIRAuth.auth() != nil {
do {
try FIRAuth.auth()?.signOut()
print("the user is logged out")
} catch let error as NSError {
print(error.localizedDescription)
print("the current user id is \(FIRAuth.auth()?.currentUser?.uid)")
}
self.logInOutlet.hidden = false
self.logOutOutlet.hidden = true
self.bookingsOutlet.hidden = true
} // end of if.. FIRAuth.auth()
}
override func viewDidLoad() {
super.viewDidLoad()
// if the user is logged out
// hide: logOut button & bookings button
// show: logIn button
// if FIRAuth.auth()?.currentUser?.uid == nil {
if FIRAuth.auth() == nil {
self.logInOutlet.hidden = false
self.logOutOutlet.hidden = true
self.bookingsOutlet.hidden = true
// it still prints the user id, why?
if let userNOTLogged = FIRAuth.auth()?.currentUser?.uid {
print("User should NOT be logged \(userNOTLogged)")
}
// if the user is logged in
// hide: logIn Button
// show: logOut button & bookings button
} else if
FIRAuth.auth() != nil {
self.logInOutlet.hidden = true
self.logOutOutlet.hidden = false
self.bookingsOutlet.hidden = false
}
}
// Log in code
FIRAuth.auth()?.signInWithEmail(email.text!, password: password.text!, completion: { (authData, error) in
let customError = error?.localizedDescription
if error != nil {
print(customError)
// display an alert with the error
self.displayAlert()
} else {
print("The user has been logged in")
//if signIn was successful, instantiate the view controller with identifier SWrevelViewidentifier
let toMenuOptions = self.storyboard?.instantiateViewControllerWithIdentifier("SWrevelViewidentifier")
self.presentViewController(toMenuOptions!, animated: true, completion: nil)
}
})
}
答案 0 :(得分:2)
我有同样的问题。由于重新启动时会发生这种情况,请尝试将此代码放入app.delegate。
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
} else {
// No user is signed in.
}
}