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

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

问题描述

我已实施 Firebase 授权以通过 Facebook 和 Google 登录我的 iOS 应用.我正在编写 Swift 代码.当应用程序启动时,我需要检查用户是否已经登录以显示正确的 ViewController(例如,如果没有人登录,则显示登录视图控制器,否则显示主视图控制器).如果我使用 Firebase 提供的简单"解决方案,则意味着

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.
  // ...
}

因此检查当前用户是否不为零,这正是 Firebase 指南 (https://firebase.google.com/docs/auth/ios/manage-users) 警报可能发生意味着

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

注意:currentUser 也可能为 nil,因为 auth 对象尚未完成初始化.如果您使用侦听器跟踪用户的登录状态,则不需要处理这种情况."

"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
  // ...
}

侦听器还将处理中间状态,以便在创建 Auth 对象时触发它.重点是我真的无法让它正常工作.任何人都可以帮助我使用此侦听器来检查用户是否已登录?

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?

谢谢

推荐答案

我是这样实现的:

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
  }
}

如果检查后不需要User对象,可以用布尔测试替换if let user = user,如下:

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):

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

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天全站免登陆