iOS:Google身份验证代码

前端之家收集整理的这篇文章主要介绍了iOS:Google身份验证代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用身份验证用户来使用与之关联的Google帐户.问题是,每次用户通过我的应用程序登录时,“允许访问”始终显示在Google的身份验证视图中,即使我已经从先前的测试中单击了“允许访问”.这是正常的还是我的代码错了?请帮帮我们.

我在out中使用以下代码进行登录

- (IBAction)signIn:(id)sender {
    if(!isSignedIn){
        [self signOutFromAll];

        NSString *keychainItemName = nil;

        // save keychain
        keychainItemName = kKeychainItemName;

        NSString *scope = @"https://www.googleapis.com/auth/plus.me";

        NSString *clientID = kClientID;
        NSString *clientSecret = kClientSecret;

        SEL finishedSel = @selector(viewController:finishedWithAuth:error:);

        GTMOAuth2ViewControllerTouch *viewController;
        viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope 
                                                                  clientID:clientID 
                                                              clientSecret:clientSecret 
                                                          keychainItemName:keychainItemName 
                                                                  delegate:self 
                                                          finishedSelector:finishedSel];

        [[self navigationController]pushViewController:viewController animated:YES]; 
    } else {
        [self displayAlertWithMessage:@"Currently Signed in."];
    } }

- (IBAction)signOut:(id)sender {
    [self signOutFromAll];
    [self displayAlertWithMessage:@"Signed out."]; }

这是代表:

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
      finishedWithAuth:(GTMOAuth2Authentication *)auth 
                 error:(NSError *)error{
    if(error != nil){
        // Authentication Failed...
        NSLog(@"Authentication error: %@",error);
        NSData *responseData = [[error userInfo] objectForKey:@"data"];
        if([responseData length] > 0)
            NSLog(@"%@",[[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]autorelease]);
        self.auth = nil;
    } else {
        // Authentication succeeded...
        isSignedIn = YES;
        self.auth = auth;
    }
}

和awakeFromNib:

- (void)awakeFromNib{
    // Fill in the Client ID and Client Secret text fields
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // First,we'll try to get the saved Google authentication,if any,from the keychain
    // Normal applications will hardcode in their client ID and client secret,// But the sample app allows the user to enter them in a text field,and saves them in the preferences
    NSString *clientID      = [defaults stringForKey:kGoogleClientIDKey];
    NSString *clientSecret  = [defaults stringForKey:kGoogleClientSecretKey];

    GTMOAuth2Authentication *auth;

    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                                 clientID:clientID
                                                             clientSecret:clientSecret];

    if (auth.canAuthorize) {
        // There is saved google authentication
        // self.serviceSegments.selectedSegmentIndex = 0;
    } 

    // Save the authentication object,which holds the auth tokens
    self.auth = auth;

    [self setAuth:auth];
    isSignedIn = self.auth.canAuthorize;
}

顺便说一句,我对这些代码的引用是在这个链接上:http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers

解决方法

来自文档:

The keychain item name is used to save the token on the user’s keychain,and should identify both your application name and the service name(s). If keychainItemName is nil,the token will not be saved,and the user will have to sign in again the next time the application is run.

http://code.google.com/p/gtm-oauth2/wiki/Introduction

因此,从您的代码中,它取决于kKeychainItemName的设置.

我以为在阅读文档时我会对此发表评论.

原文链接:https://www.f2er.com/iOS/331252.html

猜你在找的iOS相关文章