ios - Google 登录后继续

标签 ios swift segue google-signin

在 google 登录后,我无法让我的 segue 工作。对于初学者来说,我使用 SwiftKeychainWrapper 来存储用户信息。我已经将它用于 Facebook,它可以工作,但是,现在我正在设置 Google 登录,它就坏了。我尝试了几种不同的方法:将委托(delegate)放入 appDelegate 中,但是,这不允许转场,将委托(delegate)放入登录 VC 中,它就是不动。

有人可以帮忙吗!!!

import UIKit
import Firebase
import SwiftKeychainWrapper
import FBSDKCoreKit
import FBSDKLoginKit
import GoogleSignIn

class SignIn: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate, UITextFieldDelegate {



    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.emailField.delegate = self
        self.passwordField.delegate = self

        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().signInSilently()
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()!.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        }

    override func viewDidAppear(_ animated: Bool) {
        if let _ = KeychainWrapper.standard.string(forKey: KEY_UID){
            print("NOTE: ID found in keychain")
            performSegue(withIdentifier: "goToFeed", sender: nil)
        }
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (GIDSignIn.sharedInstance().hasAuthInKeychain()){
            print("signed in")
            performSegue(withIdentifier: "goToFeed", sender: self)
        }
    }

        func googleBtnTapped(_ sender: Any) {

        if let authentication = user.authentication
        {
            let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)

            Auth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
                if error != nil
                {
                    print("Problem at signing in with google with error : \(error)")

                }
                else if error == nil{
                    print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")

                }
            })
        }
    }
    

    @IBAction func facebookBtnTapped(_ sender: AnyObject) {

        let facebookLogin = FBSDKLoginManager()

        facebookLogin.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if error != nil {
                print("NOTE: Unable to authenticate with Facebook - \(String(describing: error))")
            } else if result?.isCancelled == true {
                print("NOTE: User cancelled Facebook authentication")
            } else {
                print("NOTE: Successfully authenticated with Facebook")
                let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
                self.firebaseAuth(credential)
            }
        }

    }

这几天我一直在苦苦思索,目前正在寻求任何形式的帮助。谢谢。

最佳答案

只需在 googleBtnTapped 函数中调用 GIDSignIn.sharedInstance().signIn(),如下所示:

func googleBtnTapped(_ sender: Any) {
    GIDSignIn.sharedInstance().signIn()
}

然后将身份验证代码放入 sign 函数中,如下所示:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let authentication = user.authentication {
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)

        Auth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
            if error != nil {
                print("Problem at signing in with google with error : \(error)")
            } else if error == nil {
                print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")
                print("signed in")
                performSegue(withIdentifier: "goToFeed", sender: self)
            }
        })
    }
}

关于ios - Google 登录后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46532223/

相关文章:

ios - 在 View Swift 中创建滑动动画

ios - Swift XMLMapper 解码嵌套属性

objective-c - 返回参数对象时,ARC发生内存泄漏

swift - CIDetectorTypeQRCode 给出错误

iOS8 swift : deleteRowsAtIndexPaths crashes

ios - iPhone/iPad双路录音 : headset + built-in mic

arrays - Swift 3过滤具有数组元素的对象数组

ios - 如何将数据传入和传出一系列 View Controller

ios - 将图像从 Collection View Controller 快速传递到另一个 View Controller

ios - 如何从 ViewController 传递数据以嵌入 View Controller