我已经成功地实现了Firebase的预构建UI。它包括谷歌、facebook、电子邮件和电话的提供商。启动应用程序会将您带到带有登录按钮的viewController。单击该按钮将显示authUIController
。在登录到其中一个提供程序后,authUIController
将消失,我将使用login按钮返回到原来的viewController
。如何在验证后转换到新的viewController
。我敢肯定这很简单,而且我看得太多了。请帮忙。
我尝试在代码的几个不同位置使用present(viewController(), animation: true, completion: nil)
和performSegue(withIdentifier: "goHome", sender: nil)
。我把提供者的数组从viewDidLoad()
移到viewDidAppear()
而什么也没有。我不知道在这里该怎么办。我搜索了谷歌的文档,YouTube和stack overflow都没什么运气。我知道我应该只提交我的代码片段,但是我太迷失了,以至于我应该显示我已经粘贴了viewControllers
代码。
import UIKit
import Firebase
import FirebaseAuth
import FirebaseUI
import FirebaseDatabase
import FacebookCore
import FacebookLogin
import FBSDKLoginKit
import FacebookShare
import GoogleSignIn
class LoginViewController: UIViewController, FUIAuthDelegate,
GIDSignInUIDelegate, FBSDKLoginButtonDelegate {
@IBAction func loginAction(sender: AnyObject) {
// Present the default login view controller provided by authUI
let authViewController = authUI?.authViewController();
self.present(authViewController!, animated: true, completion: nil)
}
let loginButton = FBSDKLoginButton()
fileprivate(set) var auth:Auth?
fileprivate(set) var authUI: FUIAuth? //only set internally but get externally
fileprivate(set) var authStateListenerHandle: AuthStateDidChangeListenerHandle?
override func viewDidLoad() {
super.viewDidLoad()
if let accessToken = AccessToken.current {
// User is logged in, use 'accessToken' here.
}
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signIn()
self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in
if user != nil {(self.performSegue(withIdentifier: "goHome", sender: nil))
} else {
self.loginAction(sender: self)
return
}
}
}
override func viewDidAppear(_ animated: Bool) {
let providers: [FUIAuthProvider] = [
FUIEmailAuth(),
FUIGoogleAuth(),
FUIFacebookAuth(),
FUIPhoneAuth(authUI: FUIAuth.defaultAuthUI()!),
]
//loginButton.delegate = self
// Do any additional setup after loading the view, typically from a nib.
self.auth = Auth.auth()
self.authUI = FUIAuth.defaultAuthUI()
self.authUI?.delegate = self
self.authUI?.providers = providers
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if let error = error {
// ...
return
}
// User is signed in
// ...
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("User Logged Out")
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
// ...
if let error = error {
// ...
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if let error = error {
// ...
return
}
}
}
// Implement the required protocol method for FIRAuthUIDelegate
func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
guard let authError = error else { return }
let errorCode = UInt((authError as NSError).code)
switch errorCode {
case FUIAuthErrorCode.userCancelledSignIn.rawValue:
print("User cancelled sign-in");
break
default:
let detailedError = (authError as NSError).userInfo[NSUnderlyingErrorKey] ?? authError
print("Login error: \((detailedError as! NSError).localizedDescription)");
}
}
}
最佳答案
我真不敢相信我竟然想出来了!谢谢大家的帮助!下面是我拥有的代码(这是正确的代码),但我一直收到一个错误:LoginViewController:0x104f17660>其视图不在窗口层次结构中!
func viewDidAppear(){
self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in
if user != nil {(self.performSegue(withIdentifier: "goHome", sender: nil))
} else {
self.loginAction(sender: self)
return
}
}
}
我不停地换东西。。。我试过的另一个密码是
func viewDidAppear(){
self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in
if user != nil {(self.present(UIViewController(), animated: true, completion: nil))
} else {
self.loginAction(sender: self)
return
}
}
}
不管我怎么努力,这个代码都失败了。第一个代码块是正确的,我的问题是我的故事板segue是show(push),而不是present(modally)。一旦我换了这个BAM它就成功了。呃,这么简单却又疯狂地想出来。再次感谢大家的帮助!