Is it possible to use completion handler in delegate method - Swift
Asked Answered
S

1

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()
}
Sclerite answered 28/4, 2018 at 18:54 Comment(1)
Where's the "delegate method" here? Show that, and then we can talk about how to pass isLoginSucessfull to it.Humane
A
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)
    }
}
Autolycus answered 28/4, 2018 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.