iOS firebase:未调用FIRAuthUIDelegate.authUI

时间:2016-10-25 20:37:29

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

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

enter image description here

我能够

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

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

  3. 将用户发送回原始(步骤1)

  4. 在第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重复。另一个问题的标题非常笼统,只能深入细节。无论如何,我相信克里斯的答案比那里的答案更彻底。我认为这里的问题和答案都更清晰,更尖锐,更彻底,因此如果将此标记为副本,那么将此处的人员引导到那里会是错误的。

2 个答案:

答案 0 :(得分:7)

我认为您的问题在于- (void)signInWithProviderUI:(id<FIRAuthProviderUI>)providerUI方法{/ 3}}。

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

[self.navigationController dismissViewControllerAnimated:YES completion:^{
        [self.authUI invokeResultCallbackWithUser:user error:error];
      }];
来自Apple文档的

here,预计此方法将在模态呈现的viewController上调用。您将其显示为根视图控制器。尝试使用UIViewController中的模态显示它,事情应该有效。要调试此尝试并在As you can see设置断点,以确保它不会被命中。如果在模态显示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
        }

        ...
    }

答案 1 :(得分:1)

这一定是一个参考问题。

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

的引用