2017-10-02 21 views
0

Je n'arrive pas à faire fonctionner mon segment après la connexion à google. Pour commencer, j'utilise SwiftKeychainWrapper pour stocker les informations de l'utilisateur. Je l'ai utilisé pour facebook et ça marche mais, maintenant que je configure le Google LogIn, ça casse. J'ai essayé plusieurs approches différentes: Mettre le délégué dans l'appDelegate mais, cela ne permet pas le segue, mettre le délégué dans le VC de connexion, et ça ne bouge pas.Segue Après Google Connexion

Quelqu'un peut-il aider !!!!

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) 
 
      } 
 
     } 
 

 
    }

J'ai été cogner ma tête depuis plusieurs jours et je suis à la recherche de tout type d'assistance à ce point. Merci.

Répondre

1

Il suffit d'appeler GIDSignIn.sharedInstance().signIn() dans votre fonction googleBtnTapped comme ceci:

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

puis mettre le code d'authentification dans la fonction sign comme ceci:

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) 
      } 
     }) 
    } 
} 
+0

Je vais essayer en ce moment –

+0

Yesssss ça marche! –