在iOS中通过侦听器检查Firebase当前登录的用户 [英] Checking Firebase current signed-in user via Listener in iOS

查看:208
本文介绍了在iOS中通过侦听器检查Firebase当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实施Firebase授权,通过Facebook和Google在我的iOS应用上登录。我正在编写Swift。
当应用程序启动时,我需要检查一个用户是否已经登录,以呈现正确的ViewController(例如,如果没有人登录,我提供登录视图控制器,否则我呈现主视图控制器)。
如果我使用Firebase提供的easy解决方案,则意味着如果FIRAuth.auth()?currentUser !=无{
//用户已经登录。
// ...
} else {
//没有用户登录
//。 ..

$ / code>

所以检查当前用户是不是零, Firebase指南( https://firebase.google.com/docs/auth/ ios / manage-users )可能会发生警告,意思是

注意:currentUser可能也是零,因为auth对象尚未完成初始化。监听器跟踪用户的登录状态,你不需要处理这种情况。



所以我想实现监听器指导:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' / ...
}

T监听器也会处理中间状态,以便在创建Auth对象时触发它。点是我真的不能使它正常工作。任何人都可以帮助我使用这个侦听器来检查用户是否登录?

感谢

解决方案



  FIRAuth.auth()?. addStateDidChangeListener {auth,user 
if user = user {
//用户已登录。显示主屏幕
} else {
//否用户登录。登录屏幕


code $
$ b如果你不需要用户对象在检查后,您可以用布尔测试替换,如果let user = user ,如下所示:

  FIRAuth.auth()?. addStateDidChangeListener {auth,user $ b $ if user!= nil {
//用户登录。显示主屏幕
} else {
//无用户登录。显示用户登录屏幕
}
}

在哪里放置监听器(来自注释):

对于我用来检查是否使用r已经登录了,在特定的视图控制器中,它已经足够把它放在 viewDidLoad 的开头。但是,如果您有任何需要检查每次进入特定视图控制器的情况,那么最好将它放在 viewDidAppear 的开头。但我认为在大多数情况下,只需要检查一次,如果用户进入视图


I've implement Firebase Authorization to login on my iOS app via Facebook and Google. I'm coding Swift. When the app starts up I need to check whether a user is already signed-in in order to present the proper ViewController (e.g., if nobody is signed in I present the Login View Controller, otherwise I present the Home View Controller). If I use the "easy" solution offered by Firebase, meaning

if FIRAuth.auth()?.currentUser != nil {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}

So checking if the current user is not nil, it happens exactly what the Firebase guide (https://firebase.google.com/docs/auth/ios/manage-users) alerts might happen meaning

"Note: currentUser might also be nil because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case."

So I would like to implement the listener as suggested in the guide:

handle = FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
  // ...
}

The listener will handle also intermediate status so that it is triggered when the Auth object is created. Point is I really cannot make it to work properly. Anybody can help me to use this listener in order to check if a user is logged in?

Thanks

解决方案

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

这篇关于在iOS中通过侦听器检查Firebase当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆