gpt4 book ai didi

ios - 适用于 iOS 的 Google+ SDK 以编程方式添加登录按钮

转载 作者:可可西里 更新时间:2023-11-01 03:27:25 27 4
gpt4 key购买 nike

关于 G+ 文档:https://developers.google.com/+/mobile/ios/sign-in

可以使用 XIB 或在 UIViewController 中以编程方式添加登录按钮。

我有一个 TableViewController,我将添加 G+ 登录按钮作为表格行的附属 View :

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 有一个用于登录委托(delegate)的接口(interface):

@interface MXMSettingsTableViewController () <GPPSignInDelegate>
@end

似乎未调用委托(delegate)或共享登录按钮未链接到 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 实例按钮:

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

但是这个实现中有一些我无法弄清楚的错误。

最佳答案

首先,您应该在 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,它在上面的代码片段中是一个常量值(您需要声明)

关于ios - 适用于 iOS 的 Google+ SDK 以编程方式添加登录按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18611812/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com