Custom Google Sign In throw exception on GIDSignIn

2019-06-27 00:25发布

问题:

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?

回答1:

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
 }


回答2:

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.