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)
}
}
isLoginSucessfull
to it. – Humane