1

我一直在他们的 Objective-C 库中涉足 Google 提供的 OAuth2 api。

无论如何,我很难理解如何让应用程序记住谁登录。我已经使用以下方法成功地进行了身份验证。

我调用下面的方法来展示来自 Google 的 OAuth 视图控制器,用户登录,并且我通过了身份验证。但是,每次我重新启动应用程序时,它都会再次运行登录屏幕,就好像我没有经过身份验证一样。据我了解,我需要有某种钥匙串/令牌记忆,以便它识别最近登录的用户。

但是怎么做?我无法从可用的小文档中解决这个问题。

-(void)authenticateUserFromViewController:(UIViewController *)viewController
{
    GTMOAuth2ViewControllerTouch *authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeYouTube
                                                                clientID:kClientID
                                                            clientSecret:kClientSecret
                                                        keychainItemName:kKeychainItemName
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [viewController.navigationController pushViewController:authViewController animated:YES];
}

-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
    if (error != nil)
    {
        // Authentication failed
        NSLog(@"Failed to authorise");
    }
    else
    {
        // Authentication succeeded
        NSLog(@"Authorised");
    }
}
4

1 回答 1

1

我不熟悉 Objective-C 库,但也许Google 参考的这一部分可能有用。它解释了如何使用身份验证令牌以及如何在用户重新启动应用程序时处理钥匙串。

于 2013-07-15T18:33:19.180 回答