【问题标题】:iOS firebase: FIRAuthUIDelegate.authUI not being callediOS firebase:未调用 FIRAuthUIDelegate.authUI
【发布时间】:2017-03-08 01:15:41
【问题描述】:

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

我可以

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

  2. 用户被发送到谷歌登录

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

编辑:这似乎与another question 重复。另一个问题的标题很笼统,只涉及几行深的细节。无论如何,我相信克里斯的回答比那里的回答更彻底。我认为这里的问题和答案都更清晰、更有针对性和更彻底,因此如果将这里的人们标记为重复,就会发生这种情况,因此只指导人们去那里是错误的。

【问题讨论】:

  • 你在使用故事板吗?
  • 当你说你的 authUI 没有被调用时,你的意思是 authUI 委托方法是吗?
  • 是的,就是上面代码中的那个。
  • 我认为你已经记录了错误和函数中返回的用户对象只是为了检查?

标签: swift firebase firebase-authentication google-authentication firebaseui


【解决方案1】:

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

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

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

As you can see 来自 Apple 文档,预计此方法将在模态呈现的 viewController 上调用。您将其显示为根视图控制器。尝试使用来自UIViewController 的模态显示它,事情应该会解决。要调试此尝试并在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
        }

        ...
    }

【讨论】:

  • 你能在objective c中做上面的代码吗? stackoverflow.com/questions/42340641/…
  • 怕不是@S.Sallay :/
  • 在这里遇到一个问题,尝试将 authUI 设置为 FUIAuth.defaultAuthUI(),因为它是在 applicationDidFinishLaunchingWithOptions 中执行的 FirebaseApp.configure() 方法之前执行的。有点奇怪,但是初始化 viewDidLoad() 中的值反而为我解决了这个问题。
【解决方案2】:

一定是参考的问题。

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 的引用。

【讨论】:

  • 进行您写的let authUI = FIRAuthUI.defaultAuthUI() 的确切更改导致authUI 为零,这导致“应用程序在应用程序启动结束时应该有一个根视图控制器”。我通过将引用拉到更高级别更改了我的代码authUI 仍然没有运行
  • 警告并不意味着 authUI 为 nil。这意味着窗口在应用程序启动完成之前没有 rootViewController。有两种可能。一种是 authUI.authViewController() 返回 nil,另一种可能是 authUI.authViewController() 是异步的并且需要很长时间。检查authUI.authViewController()的返回值不为零。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2018-02-02
  • 1970-01-01
  • 2018-12-12
  • 2019-11-08
  • 2020-05-07
相关资源
最近更新 更多