Checking Firebase current signed-in user via Listener in iOS

前端 未结 2 388
礼貌的吻别
礼貌的吻别 2020-12-15 07:39

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 si

2条回答
  •  死守一世寂寞
    2020-12-15 08:12

    If you're setting up the StateDidChangeListener in application:didFinishLaunchingWithOptions, you'll notice that it fires once when the listener is attached (to set the initial state, which is nil when initialising) and then again once it's finished initialising (potentially not nil). This is intended behaviour, but really impractical if you're setting it up early.

    An alternative to using the listener is using NotificationCenter. This will fire once initialisation has finished:

    NotificationCenter.default.addObserver(forName: NSNotification.Name.AuthStateDidChange, object: Auth.auth(), queue: nil) { _ in
        let user = Auth.auth().currentUser
    }
    

提交回复
热议问题