Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Google Sign In throw exception on GIDSignInDelegate protocol

I'm writing an iOS app in obj-c and using Google SignIn SDK to do the Google SignIn flow. I wanna be able to customize the button and action a little it so I went ahead implementing the protocols of GIDSignInDelegate myself based on their documentation. But it throws and exception for no reason.

Here's the minimal code of my view controller. viewcontroller.m

    #import "ViewController.h"
    #import <FBSDKLoginKit/FBSDKLoginKit.h>

    @interface ViewController ()


    @property (weak, nonatomic) IBOutlet UIButton *GoogleSignIn;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    - (IBAction)googleButtonTouchUpInside:(id)sender {
        [[GIDSignIn sharedInstance] signIn];
    }

    // Implement these methods only if the GIDSignInUIDelegate is not a subclass of
    // UIViewController.

    // Stop the UIActivityIndicatorView animation that was started when the user
    // pressed the Sign In button
    - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
        //[UIActivityIndicatorView stopAnimating];
    }

    // Present a view that prompts the user to sign in with Google
    - (void)signIn:(GIDSignIn *)signIn
    presentViewController:(UIViewController *)viewController {
        [self presentViewController:viewController animated:YES completion:nil];
    }

    // Dismiss the "Sign in with Google" view
    - (void)signIn:(GIDSignIn *)signIn
    dismissViewController:(UIViewController *)viewController {
        [self dismissViewControllerAnimated:YES completion:nil];
}

@end

viewcontroller.h

#import <UIKit/UIKit.h>
#import <Google/SignIn.h>

@interface ViewController : UIViewController <GIDSignInUIDelegate>

@end

I do have whatever delegate method is required for a custom login flow google doc Did I miss anything?

like image 498
Ian Zhao Avatar asked Nov 29 '22 23:11

Ian Zhao


2 Answers

This is basic workaround for google signIn ... so check what is missing for you

First of all in your button action use

GIDSignIn *signin = [GIDSignIn sharedInstance];
signin.shouldFetchBasicProfile = true;
signin.delegate = self;
signin.uiDelegate = self;
[signin signIn];

Then for Delegates

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
    // Perform any operations on signed in user here.
    if (error == nil) {
        NSString *userId = user.userID;                  
    } else {
        NSLog(@"%@", error.localizedDescription);
    }
}

- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error {
    // Perform any operations when the user disconnects from app here.
}

- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
    NSLog(@"%@",error.description);
}

// Present a view that prompts the user to sign in with Google
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
    //present view controller
}

// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
      //dismiss view controller
 }
like image 142
EI Captain v2.0 Avatar answered Dec 04 '22 07:12

EI Captain v2.0


Updated for Swift 3:

used below lines of code in signIn Button Action Method:

GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().signIn()

Note: Comments above lines of code if you are using it anywhere in your ViewController...! otherwise it will be fine.

like image 25
Kiran Jadhav Avatar answered Dec 04 '22 08:12

Kiran Jadhav