2

I've been working on sign in with Google account.

I basically followed the guide from Friebase (https://firebase.google.com/docs/auth/ios/google-signin)

It worked, but I have trouble performing segue to my main page from sign in page

here's the code in my signin View controller :

class SignInController: UIViewController,  GIDSignInUIDelegate{

@IBOutlet weak var signInButton: GIDSignInButton!


override func viewDidLoad() {
    super.viewDidLoad()
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signInSilently()

    }

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
    if let err = error {
        print(error)
    }
    else {
        performSegue(withIdentifier: "GoToMain", sender: self)
    }
}

it should work if i put performSegue in the signIn function but it didn't

after log in with google successfully, it goes back to protected (sign in) page

I've been working and trying to shoot this problem for almost 6 hours, still can't figure it out !!!

self learning is a lot of pain.

any help is appreciated!!! Thanks!!

Ian.

update

so now my code is like the following :

class SignInController: UIViewController,  GIDSignInUIDelegate, GIDSignInDelegate {

@IBOutlet weak var signInButton: GIDSignInButton!


override func viewDidLoad() {
    super.viewDidLoad()
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signInSilently()
    GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID


    }

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


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

        FIRAuth.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)")

            }
        })
    }
}

func signIn(signIn: GIDSignIn!, didDisconnectWithUser user: GIDGoogleUser!, withError error: NSError!) {

}

I added GIDsignDeligate protocol otherwise there will be an error at line "GIDSignIn.sharedInstance().delegate = self"

When I run it, it crashed with "unexpectedly found nil while unwrapping an Optional value error" (seem to happen at "if let authentication = user.authentication" )

Am i missing anything? I feel I'm almost there!

Ian
  • 239
  • 1
  • 14

1 Answers1

1

Just add GIDSignIn.sharedInstance().delegate = self to your viewDidLoad function:-

 override func viewDidLoad() {
  super.viewDidLoad()
   GIDSignIn.sharedInstance().uiDelegate = self
   GIDSignIn.sharedInstance().signInSilently()
   GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()!.options.clientID
   GIDSignIn.sharedInstance().delegate = self
}

And change your sign in user function to:-

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {


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

        FIRAuth.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)")

                            }
                    })
            }
}

PS:- This is swift3 version, so don't just copy paste it. Try writing it and autoComplete will help.

Dravidian
  • 9,945
  • 3
  • 34
  • 74