适用于 iOS 的 Google+ SDK 以编程方式添加登录按钮
Posted
技术标签:
【中文标题】适用于 iOS 的 Google+ SDK 以编程方式添加登录按钮【英文标题】:Google+ SDK for iOS Add Signin button programmatically 【发布时间】:2013-09-07 20:10:49 【问题描述】:至于此处的 G+ 文档:https://developers.google.com/+/mobile/ios/sign-in
可以使用 XIB 或以编程方式在 UIViewController 中添加登录按钮。
我有一个 TableViewController,我将添加 G+ 登录按钮作为表格行的附属视图:
subtitleCell.accessoryView = self.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];
// 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处设置:
- (void)viewDidLoad
[super viewDidLoad];
[self setGooglePlusButtons];
//...
UIViewControlled 有一个用于登录委托的接口:
@interface MXMSettingsTableViewController () <GPPSignInDelegate>
@end
似乎没有调用委托或共享登录按钮未链接到控制器的实例:
// 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 实例按钮:
// The button that handles Google+ sign-in.
@property (retain, nonatomic) GPPSignInButton *googlePlusSignInButton;
但是我无法弄清楚这个实现中有什么问题。
【问题讨论】:
【参考方案1】:首先,您应该在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];
而且登录方式应该是这样的:
- (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,在上面的sn-p 中它是一个常量值(您需要声明)
【讨论】:
当然可以!!!通过这种方式,我还能够将此视图与处理 G+ 帐户和登录的“控制器”对象分开(此处缺少 kClient Id 集成)。非常感谢! 奇怪的东西。 GooglePlus 应用程序打开,但没有进行身份验证,应用程序 openURL:(NSURL *)url sourceApplication: 也被调用。在开发者控制台上正确配置了clientId。 你添加了URL类型吗?这是本指南中的第 4 步:developers.google.com/+/mobile/ios/getting-started 耶! bundle Id 作为 url 类型,超级奇怪:)。现在它起作用了。非常感谢。 @loretoparisi 如果它回答了您的问题,您应该接受正确的答案【参考方案2】:尽管有几种解决方案 没有办法让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);
奇怪的东西, 钥匙串具有身份验证
2013-09-13 13:01:35.849 musiXmatch[1090:790b] MXMGooglePlusManager initialize login 0 keychain 1
但帐户未登录。
至于这里的文档
https://developers.google.com/+/mobile/ios/api/interface_g_p_p_sign_in
实现应该是正确的。
【讨论】:
以上是关于适用于 iOS 的 Google+ SDK 以编程方式添加登录按钮的主要内容,如果未能解决你的问题,请参考以下文章
发布到 App Store 后,适用于 iOS 的 Google Maps SDK 崩溃
在使用适用于 iOS 的 Google Maps SDK 和适用于 iOS 的 Google Places 时,我找不到在哪里可以获得 POI 结果
google-maps-sdk-ios - 适用于 iOS 的 Google 地图 1.4.3 的 arm 7/iphone 5 问题
适用于 iOS 的 Google Maps SDK - 折线