ios - Firebase IOS Google 登录 : Change views after successfully signed in

标签 ios swift xcode firebase

我正在尝试构建我的第一个 IOS 应用程序,并尝试按照此处的文档实现 Firebase Google 登录:https://firebase.google.com/docs/auth/ios/google-signin 。问题是如果用户已成功登录,则尝试更改 View 。我已经尝试了其他堆栈溢出帖子中有关此问题的几种解决方案,但似乎都不起作用。我相信这是因为新的 sceneDelegate 文件,其他解决方案不必考虑,因为它们使用的是以前版本的 XCode。

Google 登录是在应用程序委托(delegate)中实现的,实际尝试验证用户身份的特定代码(在应用程序委托(delegate)中)位于此处:

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
}

}

用户登录后如何更改 View ?我已经尝试了我能找到的所有方法,但似乎一切都不起作用或使应用程序崩溃。

非常感谢任何帮助

最佳答案

这是一个示例,您如何在 LoginVC 中执行此操作:

class LoginViewController: UIViewController {

  override func viewDidLoad() {

    super.viewDidLoad()

    GIDSignIn.sharedInstance().delegate = self

    GIDSignIn.sharedInstance()?.presentingViewController = self

  }
}

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

@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 用于 GoogleFacebookApple 登录。因此,您可以编写一个可以获取凭据并登录用户的函数,然后进行优化。

关于ios - Firebase IOS Google 登录 : Change views after successfully signed in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60598468/

相关文章:

ios - 在 iOS 9 中自定义 ContactPicker UI

objective-c - 创建一个包含多个 NSMutableDictionary 对象的 NSMutableArray

iOS 不应在 HTTP POST 中的 NSURLSession 的 NSURL 中添加扩展名

javascript - 将网页的功能放入 iOS 应用程序 - Swift

ios - UISearchController - Objective C 到 Swift 问题

swift - 如何快速捕获按钮事件类型

iOS - 只有一种本地化有效

ios - 如何自动将构建号添加到 Xcode ios 存档

ios - 为什么 viewForAnnotation 方法弄乱了我的用户位置?

ios - performSegue(withIdentifier.......) 不工作