3

我正在关注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);
}
4

3 回答 3

3

您的 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上看到一个工作示例

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

于 2016-05-30T21:49:27.933 回答
1

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

Obj-C:“有一个错误阻止[[FIRAuthUI authUI] authViewController]被用作应用程序的根视图控制器。解决方法是使用占位符视图控制器作为应用程序的根视图控制器,然后[[FIRAuthUI authUI] authViewController]在它上面显示。”

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

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

于 2016-06-27T03:15:49.983 回答
0

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

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

RESERVED_CLIENT_ID您可以从GoogleService-Info.plist 文件中的条目中获取您的应用 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])
}

在此处查看我的答案以获取更多详细信息。

于 2017-04-19T13:14:35.830 回答