iOS firebase:FIRAuthUIDelegate.authUI 未被调用

iOS firebase: FIRAuthUIDelegate.authUI not being called

我正在尝试从 AppDelegate.swift 启动 google 登录,然后在登录成功后启动我的应用程序的主屏幕。

我可以

  1. 显示 google 登录按钮,如上所示

  2. 用户被发送到google登录

  3. 用户返回原始状态(第 1 步)

在第 3 步之后。我想将用户转到我的应用程序的主页。

我的代码如下。 我遇到的问题是 authUI 没有被调用。

@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
    }
}

编辑:这似乎是 的副本。另一个问题的标题很笼统,只深入了几行细节。无论如何,我相信克里斯的回答比那里的回答更彻底。我认为这里的问题和答案都更清楚、更有针对性和更彻底,所以如果将这里的人标记为重复,那么直接指示这里的人去那里是错误的。

肯定是引用的问题

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

试试这个。 AppDelegate 将保存 authUI 及其 delegate.

的引用

我认为你的问题在于 here,在 - (void)signInWithProviderUI:(id<FIRAuthProviderUI>)providerUI 方法中。

dismissViewControllerAnimated:completion: 完成块中调用委托方法。

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

As you can see from the Apple docs, this method is expected to be called on a modally presented viewController. You are displaying it as a root view controller. Try displaying it with a modal from a UIViewController, and things should work out. To debug this try and set a breakpoint at line 193 看看它不会被击中。如果在模态显示 authController 时这不起作用,我会感到非常惊讶。

想出一个可能的解决方案来解决您的问题(我假设您想确保用户在使用您的应用程序之前已登录)。以下是我目前在应用程序中使用的简化。

编辑:更新了新的 1.0.0 FirebaseUI 语法。

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
        }

        ...
    }