iOS firebase: FIRAuthUIDelegate.authUI not being called

£可爱£侵袭症+ 提交于 2019-12-01 05:16:27

I think your problem lies here, in the - (void)signInWithProviderUI:(id<FIRAuthProviderUI>)providerUI method.

The delegate method is called in the dismissViewControllerAnimated:completion: completion block.

[self.navigationController dismissViewControllerAnimated:YES completion:^{
        [self.authUI invokeResultCallbackWithUser:user error:error];
      }];

As you can see from the Apple docs, this method is expected to be called on a modally presented viewController. You are displaying it as a root view controller. Try displaying it with a modal from a UIViewController, and things should work out. To debug this try and set a breakpoint at line 193 to see that it won't get hit. I would be very surprised if this doesn't work when you display the authController modally.

To come up with a possible solution to your problem (I am assuming you want to ensure a user is signed in before using your app). The below is a simplification of what I am using in an app currently.

EDIT: Updated for the new 1.0.0 FirebaseUI syntax.

class MainTabController: UITabBarController, FIRAuthUIDelegate {

    let authUI: FUIAuth? = FUIAuth.defaultAuthUI()

    override func viewDidLoad() {
        super.viewDidLoad()
        var authProviders =  [FUIFacebookAuth(), FUIGoogleAuth()]
        authUI.delegate = self
        authUI.providers = authProviders

        //I use this method for signing out when I'm developing  
        //try! FIRAuth.auth()?.signOut()

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if !isUserSignedIn() {
            showLoginView()
        }
    }

    private func isUserSignedIn() -> Bool {
      guard FIRAuth.auth()?.currentUser != nil else { return false }
      return true
    }

    private func showLoginView() {
      if let authVC = FUIAuth.defaultAuthUI()?.authViewController() {
        present(authVC, animated: true, completion: nil)
      }
   }
    func authUI(_ authUI: FUIAuth, didSignInWith user: FIRUser?, error: Error?) {
        guard let user = user else {
            print(error)
            return
        }

        ...
    }

It must be a problem of reference.

class AppDelegate: UIResponder, UIApplicationDelegate, FIRAuthUIDelegate {
    var window: UIWindow?
    let authUI = FIRAuthUI.defaultAuthUI()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        FIRApp.configure()

        authUI.delegate = self
        let providers: [FIRAuthProviderUI] = [FIRGoogleAuthUI()]
        authUI.providers = providers

       // show google login button
       let authViewController = authUI.authViewController()
       self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
       self.window?.rootViewController = authViewController
       self.window?.makeKeyAndVisible()
       return true
    }
}

Try this. AppDelegate will hold the reference of authUI and its delegate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!