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

lor*_*isi 7 ios google-plus

至于G + docs:https://developers.google.com/+/mobile/ios/sign-in

登录按钮可以使用XIB添加,也可以在UIViewController中以编程方式添加.

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

subtitleCell.accessoryView = self.googlePlusSignInButton;
Run Code Online (Sandbox Code Playgroud)

登录按钮将按如下方式初始化:

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

}
Run Code Online (Sandbox Code Playgroud)

该按钮设置在viewDidLoad:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setGooglePlusButtons];

//...
Run Code Online (Sandbox Code Playgroud)

UIViewControlled有一个登录委托的接口:

@interface MXMSettingsTableViewController () <GPPSignInDelegate>
@end
Run Code Online (Sandbox Code Playgroud)

看来代理没有被调用,或者共享登录按钮没有链接到控制器的实例:

// GPPSignInDelegate

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
                   error:(NSError *)error {
   ///....
}
Run Code Online (Sandbox Code Playgroud)

我认为

// Make sure the GPPSignInButton class is linked in because references from
// xib file doesn't count.
[GPPSignInButton class];
Run Code Online (Sandbox Code Playgroud)

是链接ViewController实例按钮:

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

但是这个实现中有些问题我无法弄清楚.

Lef*_*ris 15

首先,您应该在您的操作上调用登录方法 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];
}
Run Code Online (Sandbox Code Playgroud)

并且登录应该是这样的:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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