7

至于这里的 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;

但是我无法弄清楚这个实现有什么问题。

4

2 回答 2

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

并且登录应该是这样的:

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

于 2013-09-04T11:20:32.400 回答
0

尽管有多种解决方案,但仍无法使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

实施应该是正确的。

于 2013-09-13T11:05:33.443 回答