How to use FirebaseUI to authenticate Google in iOS in Swift?

I follow https://firebase.google.com/docs/auth/ and want to use FirebaseUI ( https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseUI ) for authentication.

The user interface shows up successfully, and I can click "log in with Google" and then complete the web login stream. The application opens again with the auth url, but the authUI function never fires. What's wrong?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. FIRApp.configure() let authUI = FIRAuthUI.authUI()!; NSLog("setting up delegate"); authUI.delegate = self; let googleAuthUI = FIRGoogleAuthUI.init(clientID:FIRApp.defaultApp()!.options.clientID); authUI.signInProviders = [googleAuthUI!]; mSplitViewController = self.window!.rootViewController as! UISplitViewController self.window!.rootViewController = authUI.authViewController(); return true } func authUI(authUI: FIRAuthUI, didSignInWithUser user: FIRUser?, error:NSError?) { // Implement this method to handle signed in user or error if any. NSLog("logged in"); self.window!.rootViewController = mSplitViewController let navigationController = mSplitViewController!.viewControllers[mSplitViewController!.viewControllers.count-1] as! UINavigationController navigationController.topViewController!.navigationItem.leftBarButtonItem = mSplitViewController!.displayModeButtonItem() mSplitViewController!.delegate = self let masterNavigationController = mSplitViewController! .viewControllers[0] as! UINavigationController let controller = masterNavigationController.topViewController as! MasterViewController controller.managedObjectContext = self.managedObjectContext } func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { NSLog("opened with url \(url)"); FIRAuthUI.authUI()!.delegate = self; return FIRAuthUI.authUI()!.handleOpenURL(url, sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String); } 
+2
source share
3 answers

I have not tried this solution, but this StackOverflow problem was related to the FirebaseUI repo problems section and someone answered there;

Obj-C: โ€œThere's a bug that prevents using [[FIRAuthUI authUI] authViewController] as the controller of the root view of your application. The workaround is to use the placeholder view controller as the view controller of the root application of your application and then submit [[FIRAuthUI authUI] authViewController] on top of it. "

for Swift users: There is an error that prevents the use of FIRAuthUI.authUI().authViewController() as the controller of the root view of your application. The workaround is to use the placeholder view controller as the view controller for the root application of your application, and then present FIRAuthUI.authUI().authViewController() on top of it.

Link: https://github.com/firebase/FirebaseUI-iOS/issues/65

+1
source

Is your AppDelegate a FIRAuthUIDelegate?

Anyway, instead of using delegates, you can use the FIRAuth listener: func addAuthStateDidChangeListener (listener: FIRAuthStateDidChangeListenerBlock) -> FIRAuthStateDidChangeListenerHandle

You can use it as follows:

 FIRAuth.auth()?.addAuthStateDidChangeListener { (auth, user) in if user != nil { print("user signed in") } } 

You can see the working sample https://github.com/cooliopas/FirebaseAuth-Demo

In Spanish, but I'm sure you will understand the code.

+2
source

Essentially you need to add below to the plist root.

 <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.your-app-id</string> </array> </dict> </array> 

You can get your application ID from the RESERVED_CLIENT_ID entry in the GoogleService-Info.plist file.

Then you will need to implement the openURL application delegation method as follows:

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) } 

See my answer here for more details.

0
source

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


All Articles