Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ SDK for iOS Add Signin button programmatically

As for G+ docs here: https://developers.google.com/+/mobile/ios/sign-in

The sign in button can be added using a XIB or programmatically in a UIViewController.

I have a TableViewController and I'm going to add the G+ Signin button as the accessory view of a table row:

subtitleCell.accessoryView = self.googlePlusSignInButton;

where the signin button is going to be initialized as follows:

-(void) setGooglePlusButtons {

    self.googlePlusSignInButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];


    UIImage *backgroundButtonImage = [UIImage imageNamed:@"bt_search_cancel.png"];

    googlePlusSignInButton_.frame = CGRectMake(0.0f,
                                               0.0f,
                                               backgroundButtonImage.size.width,
                                               backgroundButtonImage.size.height);

    googlePlusSignInButton_.titleLabel.textColor = [UIColor whiteColor];
    googlePlusSignInButton_.titleLabel.font = [UIFont boldSystemFontOfSize:11.0f];
    googlePlusSignInButton_.titleLabel.numberOfLines = 2;

    googlePlusSignInButton_.titleLabel.shadowColor = [UIColor darkGrayColor];
    googlePlusSignInButton_.titleLabel.shadowOffset = CGSizeMake(0.0f,
                                                                 -1.0f);

    [googlePlusSignInButton_ setTitle:NSLocalizedString(@"UI_BUTTONS_LOGIN", @"")
                             forState:UIControlStateNormal];

    [googlePlusSignInButton_ setBackgroundImage:backgroundButtonImage
                                       forState:UIControlStateNormal];


    // Make sure the GPPSignInButton class is linked in because references from
    // xib file doesn't count.
    [GPPSignInButton class];

    GPPSignIn *signIn = [GPPSignIn sharedInstance];

    signIn.delegate = self;
    signIn.shouldFetchGoogleUserEmail = signIn.shouldFetchGoogleUserEmail;
    signIn.actions = [NSArray arrayWithObjects:
                      @"http://schemas.google.com/ListenActivity",
                      nil];

}

The button is set up at the viewDidLoad:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setGooglePlusButtons];

//...

The UIViewControlled has an interface for the signin delegate:

@interface MXMSettingsTableViewController () <GPPSignInDelegate>
@end

It seems that the delegate is not being called or the shared sign in button is not linked to the instance of the controller:

// GPPSignInDelegate

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
                   error:(NSError *)error {
   ///....
}

I assume that

// Make sure the GPPSignInButton class is linked in because references from
// xib file doesn't count.
[GPPSignInButton class];

is linking the ViewController instance button:

// The button that handles Google+ sign-in.
@property (retain, nonatomic) GPPSignInButton *googlePlusSignInButton;

but there is something wrong in this implementation I cannot figure out.

like image 337
loretoparisi Avatar asked Sep 04 '13 10:09

loretoparisi


1 Answers

First of all you should call the sign in method on a action of your googlePlusSignInButton

So the code should be:

-(void) setGooglePlusButtons {

    self.googlePlusSignInButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];


    UIImage *backgroundButtonImage = [UIImage imageNamed:@"bt_search_cancel.png"];

    googlePlusSignInButton_.frame = CGRectMake(0.0f,
                                               0.0f,
                                               backgroundButtonImage.size.width,
                                               backgroundButtonImage.size.height);

    googlePlusSignInButton_.titleLabel.textColor = [UIColor whiteColor];
    googlePlusSignInButton_.titleLabel.font = [UIFont boldSystemFontOfSize:11.0f];
    googlePlusSignInButton_.titleLabel.numberOfLines = 2;

    googlePlusSignInButton_.titleLabel.shadowColor = [UIColor darkGrayColor];
    googlePlusSignInButton_.titleLabel.shadowOffset = CGSizeMake(0.0f,
                                                                 -1.0f);

    [googlePlusSignInButton_ setTitle:NSLocalizedString(@"UI_BUTTONS_LOGIN", @"")
                             forState:UIControlStateNormal];

    [googlePlusSignInButton_ setBackgroundImage:backgroundButtonImage
                                       forState:UIControlStateNormal];

    [googlePlusSignInButton addTarget:self action:@selector(signInGoogle:) forControlEvents:UIControlEventTouchUpInside];
}

and the sign in should be this way:

- (void)signInGoogle {
    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.delegate = self;
    signIn.shouldFetchGoogleUserEmail = YES;
    signIn.clientID = kClientID;
    signIn.scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusLogin,nil];
    signIn.actions = [NSArray arrayWithObjects:@"http://schemas.google.com/ListenActivity",nil];
    [signIn authenticate];
}

Your code was missing the [signIn authenticate]; call, plust you need to also pass in your clientID, which in the above snippet is a constant value (you need to declare that)

like image 164
Lefteris Avatar answered Sep 30 '22 18:09

Lefteris