适用于iOS的Google+ SDK以编程方式添加登录按钮 [英] Google+ SDK for iOS Add Signin button programmatically
问题描述
至于G +文档: https://developers.google.com/ + / mobile / ios / sign-in
登录按钮可以使用XIB添加,也可以在UIViewController中以编程方式添加。
The sign in button can be added using a XIB or programmatically in a UIViewController.
我有一个TableViewController,我将添加G + Signin按钮作为表格行的附件视图:
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];
}
按钮设置在viewDidLoad:
The button is set up at the viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[self setGooglePlusButtons];
//...
UIViewControlled有一个登录界面委托:
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 {
///....
}
我认为
// Make sure the GPPSignInButton class is linked in because references from
// xib file doesn't count.
[GPPSignInButton class];
链接ViewController实例按钮:
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.
推荐答案
首先,您应该在 googlePlusSignInButton
所以代码应为:
-(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];
}
您的代码缺少 [signIn authenticate];
调用,你需要传入你的clientID,在上面的代码片段中是一个常量值(你需要声明)
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)
这篇关于适用于iOS的Google+ SDK以编程方式添加登录按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!