Google登录后的Segue

问题描述 投票:2回答:1

谷歌登录后我无法让我的segue工作。首先,我使用SwiftKeychainWrapper来存储用户信息。我已经将它用于Facebook并且它可以工作但是,现在我正在设置Google LogIn,它会中断。我已经尝试了几种不同的方法:将委托放入appDelegate但是,这不允许segue,将委托放入登录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)
            }
        }

    }

我已经好几天了,现在我正在寻找各种帮助。谢谢。

ios swift segue google-signin google-login
1个回答
2
投票

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

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)
            }
        })
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.