Firebase IOS Google登录:成功登录后更改视图 [英] Firebase IOS Google Sign in: Change views after successfully signed in

查看:67
本文介绍了Firebase IOS Google登录:成功登录后更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建我的第一个IOS App,并尝试按照位于此处的文档实施Firebase Google登录:

I'm trying to build my first IOS App and trying to implement Firebase Google sign-in following the documentation located here: https://firebase.google.com/docs/auth/ios/google-signin. The issue is trying to change views if a user has successfully signed in. I have tried several solutions regarding this problem from other Stack overflow posts with none seeming to work. I believe this is because of the new sceneDelegate file which the other solutions don't have to take into account as they're using previous versions of XCode.

Google登录是在App委托中实现的,而实际尝试验证用户身份的特定代码(在App委托中)在此处:

The Google sign-in is implemented in the App Delegate and the specific code that actually tries to authenticate a user (In the App delegate) is here:

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().signIn(with: credential) { (authResult, error) in
  if let error = error {
    // ...
    return
  }
  // User is signed in
  // Here i want to change views
}

}

用户登录后,如何更改视图?我已经尝试了所有可以找到的内容,但一切似乎都无法正常工作或使应用程序崩溃.

Once the user is signed in how do I change views? I have tried everything I can find and everything seems to not work or crash the app.

非常感谢您的帮助

推荐答案

下面是一个示例,您如何在 LoginVC 中完成此操作:

Here is an example, how can you do it in your LoginVC:

class LoginViewController: UIViewController {

  override func viewDidLoad() {

    super.viewDidLoad()

    GIDSignIn.sharedInstance().delegate = self

    GIDSignIn.sharedInstance()?.presentingViewController = self

  }
}

这是我在 viewDidLoad 方法中添加的内容.之后,我创建了一个自定义按钮,该按钮继承了 UIButton .我已经以编程方式创建了该按钮,但是您可以只使用 @IBOutlet弱变量googleB:UIButton!.然后,在该按钮的点击上,您可以添加以下内容:

This is what I have added in the viewDidLoad method. After that, I have created a custom button which inherits to UIButton. I have created that button programmatically but you can just use @IBOutlet weak var googleB: UIButton!. Then, on that button's tap, you can add this:

@IBAction func googleBTap(_ sender: UIButton) {

    GIDSignIn.sharedInstance().signIn()
}

然后,我为 GIDSignInDelegate 创建了扩展名.

extension LoginViewController: GIDSignInDelegate {

 func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

    if let error = error {

        print(error)

        return
    }

    guard let email = user.profile.email else { return }

    guard let authentication = user.authentication else { return }

      let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                accessToken: authentication.accessToken)
  Auth.auth().signIn(with: credential) { (authResult, error) in
    if let error = error {
     print(error)
      return
    }

    //If login is successful then add
   self.navigationController.pushViewController(nextVC(), animated: true) 
   //OR
   self.performSegue() //<-- Whatever goes in here, I don't use these methods as I create views programitacally, but you can use it here and move to next view 
}

 }

}

这是一种方法,但是随着您的应用程序变大,最好创建一个 FirebaseHelper 类,并且您将使用许多常见的方法,例如 fetchProviders signIn (用于 Google Facebook Apple 登录).因此,您可以编写一个函数来获取凭证并登录用户,然后对该函数进行优化.

Well, this is one way, but as your app goes bigger it is better to create a FirebaseHelper class and you are going to use a lot of common methods like fetchProviders, signIn for Google, Facebook, Apple sign-ins. So, you can write one function which can fetch the credential and log in the user, that will be optimized then.

这篇关于Firebase IOS Google登录:成功登录后更改视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆