iOS firebase: FIRAuthUIDelegate.authUI not being called

后端 未结 2 782
孤城傲影
孤城傲影 2021-01-12 20:58

I am trying to launch google login from AppDelegate.swift and then launch my app\'s main screen upon login success.

I am able to

  1. show the g

相关标签:
2条回答
  • 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.

    0 讨论(0)
  • 2021-01-12 21:41

    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
            }
    
            ...
        }
    
    0 讨论(0)
提交回复
热议问题