Bhavuk Jain Bhavuk Jain - 11 months ago 53
Swift Question

Delegate method is not called in a custom class

This is a wrapper class I've made for google login.

class GoogleLoginWrapper:NSObject, GIDSignInDelegate {

weak var presentingController:UIViewController!

init(presentingController:UIViewController) {
self.presentingController = presentingController
var configureError:NSError?
GIDSignIn.sharedInstance().delegate = self

// Google Delegate Methods
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {


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

And I'm calling this class from a view controller class as follows:

class ViewController: UIViewController,GIDSignInUIDelegate{

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

let googleWrapper = GoogleLoginWrapper(presentingController: self)

//IBAction method to call google login.


This opens the google authentication on safari but the problem is the delegate method is never called in GoogleLoginWrapper class. But if I put the same code defined in GoogleLoginWrapper in Appdelegate, it works. Why is it happening?

Answer Source

Your instance of GoogleLoginWrapper goes out of scope at the end of your viewDidLoad method so it is deallocated immediately. You can't call the sign... function with the code you posted.

You should use a property instead of a local variable to keep a reference to the GoogleLoginWrapper instance.