【发布时间】:2018-12-30 10:09:55
【问题描述】:
我正在尝试在我的应用程序中配置 addStateDidChangeListener。我在Firebase Docs 中读到,我应该能够在ViewController 的viewWillAppear() 方法中附加监听器,并在ViewController 的viewWillDisappear() 方法中分离监听器。是否可以在AppDelegate 中进行设置,因为我的应用需要在许多不同的ViewControllers 中检查用户?
我原本想在didFinishLaunchingWithOptions()方法中附加监听器,但是在StackOverflow上找到了关于这个here的答案。我对这个答案的代码如何让我在ViewController 中进行检查感到有些困惑。代码如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
NotificationCenter.default.addObserver(forName: NSNotification.Name.AuthStateDidChange, object: Auth.auth(), queue: nil) { (_) in
let user = Auth.auth().currentUser
}
return true
}
首先,上述代码是否被认为是设置以下 Firebase 方法的正确方法:
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
// ...
}
其次,如何在我的ViewControllers 中访问这个NSNotification?
第三,我在哪里分离NSNotification 和监听器?是在applicationWillTerminate() 方法、applicationWillResignActive() 方法还是其他方法中?
【问题讨论】:
标签: ios swift firebase-authentication appdelegate nsnotifications