我正在关注 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/