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

标签 ios swift firebase firebase-authentication firebaseui

我正在关注 https://firebase.google.com/docs/auth/并希望使用 FirebaseUI ( https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseUI ) 进行身份验证。

UI 显示成功,我可以点击“使用 google 登录”,然后完成网络登录流程。该应用程序使用 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 监听器而不是使用委托(delegate):func addAuthStateDidChangeListener(listener: FIRAuthStateDidChangeListenerBlock) -> FIRAuthStateDidChangeListenerHandle

你可以这样使用它:

FIRAuth.auth()?.addAuthStateDidChangeListener {

    (auth, user) in

    if user != nil {

        print("user signed in")

    }

}

您可以在 https://github.com/cooliopas/FirebaseAuth-Demo 上看到一个工作示例

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

关于ios - 如何在 Swift 的 iOS 上使用 FirebaseUI 进行 Google 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426176/

相关文章:

ios - MKPolyline 仅在 map 移动时显示

swift - 如何修复 "Expression was too complex to be solved in reasonable time"?

firebase - 将用户添加到 Firebase 数据库

ios - 如何在按下按钮时转到 tableView 的顶部

ios - 在 swift 4 中添加 uiview 后按钮不起作用

ios - UINavigationbar 通过外观设置阴影不起作用

ios - 用字典数据填充 UITableView (Swift)

javascript - 部署到 firebase 托管的 ReactJS 应用程序在首次部署后未更新

javascript - 类型错误 : Cannot Read Property child of undefined

ios - Xcode - swift ;为启动屏幕添加音效