iOS版Google+ SDK以编程方式添加loginbutton

至于G +文档在这里: https : //developers.google.com/+/mobile/ios/sign-in

loginbutton可以使用XIB或编程方式添加到UIViewController中。

我有一个TableViewController,我将添加G + Signinbutton作为表行的附件视图:

subtitleCell.accessoryView = self.googlePlusSignInButton; 

loginbutton将被初始化如下:

 -(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]; } 

该button设置在viewDidLoad:

 - (void)viewDidLoad { [super viewDidLoad]; [self setGooglePlusButtons]; //... 

UIViewControlled有一个login委托的接口:

 @interface MXMSettingsTableViewController () <GPPSignInDelegate> @end 

看来代理没有被调用,或共享loginbutton没有链接到控制器的实例:

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

我假设

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

正在链接ViewController实例button:

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

但是在这个实现中有一些问题我不明白。

首先,您应该调用您的googlePlusSignInButton操作的login方法

所以代码应该是:

 -(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]; } 

login应该是这样的:

 - (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]; } 

你的代码缺less[signIn authenticate]; 调用,plust你还需要传递你的clientID,在上面的代码片段中是一个常量值(你需要声明)

尽pipe有几个解决scheme,但无法使silentAuthentication正常工作

 - (void)initialize { // Read Google+ deep-link data. [GPPDeepLink setDelegate:self]; [GPPDeepLink readDeepLinkAfterInstall]; // Setup Google+ share dialog. [GPPShare sharedInstance].delegate = self; // Setup Google+ signin [GPPSignIn sharedInstance].clientID = APP_GOOGLEPLUS_APPID; [GPPSignIn sharedInstance].delegate = self; [GPPSignIn sharedInstance].shouldFetchGoogleUserEmail = YES; [GPPSignIn sharedInstance].shouldFetchGoogleUserID = YES; [GPPSignIn sharedInstance].actions = [NSArray arrayWithObjects:[self momentSchemaByType:MXMGooglePlusMomentListen], nil]; [GPPSignIn sharedInstance].scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil]; // defined in GTLPlusConstants.h [[GPPSignIn sharedInstance] trySilentAuthentication]; shareAfterLogin= NO; trackIdToShare = nil; if([[MXMLogger sharedLogger] isDebug]) { NSLog(@"MXMGooglePlusManager initialize login %d keychain %d", [self isGooglePlusLoggedin], [GPPSignIn sharedInstance].hasAuthInKeychain); } } 

奇怪的是,钥匙扣在中有authentication

 2013-09-13 13:01:35.849 musiXmatch[1090:790b] MXMGooglePlusManager initialize login 0 keychain 1 

但该帐户未login。

至于这里的文档

https://developers.google.com/+/mobile/ios/api/interface_g_p_p_sign_in

实施应该是正确的。