IOS firebase: FIRAuthUIDelegate.authUI not called

I am trying to launch google login from AppDelegate.swift and then launch the main application screen after a successful login.

enter image description here

I can

  • show google login button as above

  • user goes to Google to log in

  • the user is sent back to the original (step 1)

After step 3. I want to send the user to the main page of my application.

My code is below. The problem I am facing is that authUI not being called.

 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, FIRAuthUIDelegate { var window: UIWindow? var authUI: FIRAuthUI? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { FIRApp.configure() authUI = FIRAuthUI.defaultAuthUI() 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 } func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey]) } func authUI(authUI: FIRAuthUI, didSignInWithUser user: FIRUser?, error: NSError?) { // launch main view controller } } 

EDIT: This seems to be a duplicate of another question . Another name for the question is quite general, and only the details reach several lines. In any case, I think Chris’s answer is more thorough than the one that’s there. I think that both the question and the answers here are clearer, more pointed and more thorough, so it would be a mistake to just send people here there, as it would if it were marked as a duplicate.

+5
source share
2 answers

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

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

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

As you can see from the Apple docs, it is expected that this method will be called on a modally represented viewController. You show it as the root view controller. Try displaying it with a modal from the UIViewController , and everything should work out. To debug this attempt and set a breakpoint on line 193 to see that it doesn't hit. I would be very surprised if this does not work when you automatically show authController.

Come up with a possible solution to your problem (I assume that you want the user to be signed before using your application). The following is a simplification of what I am currently using in the application.

EDIT : Updated for the new FirebaseUI syntax 1.0.0.

 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 } ... } 
+5
source

This should be a link issue.

 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 it. AppDelegate will contain the authUI link and its delegate .

+1
source

Source: https://habr.com/ru/post/1258773/


All Articles