6

I am trying to handle "google sign in" in singleton helper class.

I have LoginHelper, and a method which handles logins with completion handler. As you know Google Sign have delegate methods. When delegate methods are called I need to notify my completion handler. I am not sure, is it possible?

Let's brainstorm together or give me a hand.

My method is as follows;

 @objc func googleLoginPressed(viewController:UIViewController, isLoginSuccessfull:(Bool) -> ())
{        
    GIDSignIn.sharedInstance().signIn()
}
1
  • Where's the "delegate method" here? Show that, and then we can talk about how to pass isLoginSucessfull to it.
    – matt
    Commented Apr 28, 2018 at 19:01

1 Answer 1

9

Yes, it is possible

You have to set typeable:

public typealias isCompletion = (_ isConnected: Bool?) -> Void

And add variables like this in class:

var completion: isCompletion?

Your method like this:

func googleLoginPressed(viewController:UIViewController, isLoginSuccessfull:@escaping isCompletion){
    completion = isLoginSuccessfull
    GIDSignIn.sharedInstance().signIn()
}

And after login delegate method will call and then do like this:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {
        completion(true)
    }
}

Or

You can do this using protocols:

import UIKit
import GoogleSignIn

/// delegates to handle success and failure response of google sign-in
protocol LoginWithGoogleDelegate: class {
    func didSucceedToSignInFor(user: UserModel)
    func didFailToSignIn(error: Error)
}

/// separate class for google sign-in methods
class LoginWithGoogle: NSObject {

    // MARK: - Properties
    static let sharedInstance = LoginWithGoogle()
    weak var delegate: LoginWithGoogleDelegate?
    var globalViewController: UIViewController?

    // MARK: - Helper Methods
    /**
    configures the settings of google sign-in sdk
     - parameter viewController: it is the view controller on which we have to show the google sign-in screen
    */
    func configureGooglePlus(viewController: UIViewController) {
        globalViewController = viewController
        GIDSignIn.sharedInstance().clientID = Configuration.GoogleSignIn.clientID
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().signIn()
    }
}

// MARK: - GIDSignInDelegate methods
extension LoginWithGoogle: GIDSignInDelegate {

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("\(error.localizedDescription)")
            self.delegate?.didFailToSignIn(error: error)
        } else {
            // Perform any operations on signed in user here.
            var googleUser = UserModel()
            googleUser.googlePlusId = user.userID
            googleUser.googlePlusToken = user.authentication.idToken
            googleUser.fullName = user.profile.name
            googleUser.email = user.profile.email

            //send the user details through LoginWithGoogleDelegate method
            self.delegate?.didSucceedToSignInFor(user: googleUser)
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        print("Something Went Wrong")
        self.delegate?.didFailToSignIn(error: error)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.