How to use FirebaseUI for Google authentication on iOS in Swift?

情到浓时终转凉″ 提交于 2019-12-20 01:42:13

问题


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

The UI shows successfully and I'm able to click "sign in with google" and then complete the web sign in flow. The app re-opens 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);
}

回答1:


I haven't tried this solution but this StackOverflow problem was linked to the FirebaseUI repo's issues section and someone there responded;

Obj-C: "There's a bug that prevents [[FIRAuthUI authUI] authViewController] from being used as the root view controller of your app. The workaround is to use a placeholder view controller as your app's root view controller, then present [[FIRAuthUI authUI] authViewController] on top of it."

for Swift users: There's a bug that prevents FIRAuthUI.authUI().authViewController() from being used as the root view controller of your app. The workaround is to use a placeholder view controller as your app's root view controller, then present FIRAuthUI.authUI().authViewController() on top of it.

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




回答2:


Is your AppDelegate a FIRAuthUIDelegate?

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

You can use it this way:

FIRAuth.auth()?.addAuthStateDidChangeListener {

    (auth, user) in

    if user != nil {

        print("user signed in")

    }

}

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

Its in spanish, but I'm sure you will understand the code.




回答3:


Essentially you need to add the below to the root of the plist.

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

You can get your app id from the RESERVED_CLIENT_ID entry in your GoogleService-Info.plist file.

Next, you will need to implement the openURL app delegate method like this:

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])
}

Check out my answer here for some more details.



来源:https://stackoverflow.com/questions/37426176/how-to-use-firebaseui-for-google-authentication-on-ios-in-swift

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