【发布时间】:2021-01-22 09:58:40
【问题描述】:
所以我有这个情节提要,如果用户未登录,我希望向用户显示登录视图控制器,否则显示标签视图控制器。 这是我在应用程序委托中的代码:
`
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
UITabBar.appearance().tintColor = .white
// check user
let authListener = Auth.auth().addStateDidChangeListener { auth, user in
if user != nil {
print("USER IS NOT NIL")
userService.observeUserProfile(uid: user!.uid) { userProfile in
userService.currentUserProfile = userProfile
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "TabBarVC") as! UITabBarController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
} else {
print("USER IS NIL")
userService.currentUserProfile = nil
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "FirstVC") as! UIViewController
vc.modalPresentationStyle = .fullScreen
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
}
}
` 我的问题是,如果用户没有登录,它看起来像这样:
您可以看到视图不是全屏的,即使我在应用程序委托中指定了它,所以用户可以简单地关闭它并进入主屏幕。同样在标签视图控制器中,我有一个播放视频的主屏幕,登录屏幕在后台也有一个播放视频,两个视频同时播放 XD。欢迎提供任何提示。
【问题讨论】:
-
我可以立即发现的问题之一是您当前的用户检测逻辑是异步的,这意味着需要时间来完成......即使这不是您问题的根源,也绝对是您可能会考虑更改的内容。
-
谢谢,我现在明白为什么我遇到了一些问题。我怎样才能让它同步?
-
对此没有单一的“正确”答案,因为它取决于很多因素。一种常见的方法是尝试“自动登录”,显示某种形式的加载指示器,然后决定何时从身份验证服务获得响应。