且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iOS中通过侦听器检查Firebase当前登录的用户

更新时间:2023-12-01 16:44:28

I've implemented it like this:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if let user = user {
    // User is signed in. Show home screen
  } else {
    // No User is signed in. Show user the login screen
  }
}

If you don't need the User object after checking, you can replace if let user = user with a boolean test, like this:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if user != nil {
    // User is signed in. Show home screen
  } else {
    // No User is signed in. Show user the login screen
  }
}

Where to put the listener (from the comments):

For the cases I used to check if a user is signed in, it was enough to put it at the beginning of viewDidLoad in the specific view controller. But if you have any cases where you need to check every time you enter the specific view controller then it would be better to put it at the beginning of viewDidAppear. But I think in most cases you need to check only once, if the user enters the view

相关阅读

推荐文章