自动登录用户喜欢Instagram-Swift



我正在为我的应用程序使用firebase,我正试图像Instagram和其他社交媒体应用程序一样自动登录到用户。我尝试使用UserDefaults,firebase currentUser,但在重新启动应用程序后,它不记得用户了

class AppDelegate: UIResponder, UIApplicationDelegate {
var db: Firestore!
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()    
if let _ = Auth.auth().currentUser {
let homeController = mainViewController()
self.window?.rootViewController = homeController
self.window?.makeKeyAndVisible()
} else {
let rootController = loginViewController()
self.window?.rootViewController = rootController
self.window?.makeKeyAndVisible()  
}
return true
}
}

Firebase模块是异步的,重新授权确实需要时间,用户可能需要时间。这意味着在身份验证状态最终确定之前访问currentUser将导致用户看起来没有被加载。

相反,您应该监听Auth状态。您可以在这里找到swift的文档:https://firebase.google.com/docs/auth/ios/start#listen_for_authentication_state

最新更新