如何在 Swift 中的 iOS 上使用 FirebaseUI 进行 Google 身份验证?

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

我正在关注 https://firebase.google.com/docs/auth/ and want to use FirebaseUI (https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseUI) 进行身份验证。

UI 显示成功,我可以单击 "sign in with google",然后完成 Web 登录流程。该应用程序使用 auth url 重新打开,但 authUI 函数永远不会触发。怎么了?

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

您的 AppDelegate 是 FIRAuthUIDelegate 吗?

无论如何,您可以使用 FIRAuth 侦听器而不是使用委托:func addAuthStateDidChangeListener(listener: FIRAuthStateDidChangeListenerBlock) -> FIRAuthStateDidChangeListenerHandle

你可以这样使用:

FIRAuth.auth()?.addAuthStateDidChangeListener {

    (auth, user) in

    if user != nil {

        print("user signed in")

    }

}

您可以在 https://github.com/cooliopas/FirebaseAuth-Demo

上看到工作示例

它是西班牙语,但我相信您会理解代码。

我还没有尝试过这个解决方案,但是这个 Whosebug 问题与 FirebaseUI 存储库的问题部分相关联并且那里有人做出了回应;

目标-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."

Swift 用户: 有一个错误阻止 FIRAuthUI.authUI().authViewController() 被用作您的应用程序的根视图控制器。解决方法是使用占位符视图控制器作为应用程序的根视图控制器,然后在它上面显示 FIRAuthUI.authUI().authViewController()

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

基本上您需要将以下内容添加到 plist 的根目录。

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

您可以从 GoogleService-Info.plist 文件中的 RESERVED_CLIENT_ID 条目获取您的应用程序 ID。

接下来,您需要像这样实现 openURL 应用委托方法:

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

查看我的回答 here 了解更多详情。