4

我正在编写一个应该显示当前 AdSense 数据的应用程序,我的问题是每次打开应用程序时都会提示 GTMAuth2ViewControllerTouch,所以熟悉的 Google OAuth 2.0 登录屏幕(我用来做登录的东西)。我怎样才能登录一次,然后用户永远登录?在教程中有一个kKeychainItemName,但它只是一个字符串,我怎样才能使用它来自动登录用户?看看 ViewDidLoad。

那是我使用的代码:

#import "ViewController.h"
#import "GTMOAuth2Authentication.h"
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTMOAuth2SignIn.h"
#import "GTMHTTPFetcher.h"

static NSString *const kKeychainItemName = @"OAuth2 AdZen: AdSense";
NSString *kMyClientID = @"xxxxxxxxx.apps.xxxxxxxxxxxxx.com";     // pre-assigned by service
NSString *kMyClientSecret = @"xxxxxxxxxx--xxxxx"; // pre-assigned by service

NSString *scope = @"https://www.googleapis.com/auth/adsense.readonly"; // scope for Google+ API


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (/*USER ALREADY LOGGED IN*/) {
    //DO THE LOGIN STUFF AUTOMATICALLY WITH THE KEYCHAINITEM
}
else
{

[self signInToGoogle];

}


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)signInToGoogle
{
   GTMOAuth2ViewControllerTouch *viewController;
    viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                                                 clientID:kMyClientID
                                                             clientSecret:kMyClientSecret
                                                         keychainItemName:kKeychainItemName
                                                                 delegate:self
                                                         finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [self.navigationController pushViewController:viewController animated:YES];

}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error {
    if (error != nil) {
        // Authentication failed
        UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"AdZen"
                                                            message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:@"Try again", nil];
        fail.tag = 1;

        [fail show];
        NSLog(@"Authentication failed!");
    } else {
        // Authentication succeeded
        UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"AdZen"
                                                       message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                      delegate:self
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
        success.tag = 2;

        [success show];
        NSLog(@"Autzentication succeeded!");

    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        if (alertView.tag == 1) {
            [self signInToGoogle];
        }
    }



}

- (void)authentication:(GTMOAuth2Authentication *)auth
               request:(NSMutableURLRequest *)request
     finishedWithError:(NSError *)error {
    if (error != nil) {
        NSLog(@"Authentication failed! 1");
    } else {
        // Authentication succeeded

        NSLog(@"Autzentication succeeded! 1");

    }

}



- (void)awakeFromNib {
    // Get the saved authentication, if any, from the keychain.
    GTMOAuth2Authentication *auth;
    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                                 clientID:kMyClientID
                                                             clientSecret:kMyClientSecret];

    // Retain the authentication object, which holds the auth tokens
    //
    // We can determine later if the auth object contains an access token
    // by calling its -canAuthorize method
    //[self setAuthentication:auth];
    //NSLog(@"Already logged in!");
}

-(IBAction)signout
{

    [GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];

    UIAlertView *signedout = [[UIAlertView alloc] initWithTitle:@"AdZen"
                                                      message:@"You just signed out, please sign in to see your AdSense info."
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];    
    [signedout show];

    [self signInToGoogle];

}
4

1 回答 1

2

在“awakeFromNib:”方法中,检查身份验证以了解用户是否已经登录。如果用户已经登录,请导航到您希望在登录后将用户带到的下一个视图或下一个屏幕。

-(void)awakeFromNib {
    GTMOAuth2Authentication *auth = nil;
    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                             clientID:kMyClientID
                                                         clientSecret:kMyClientSecret];

   //Retain the authentication object, which holds the auth tokens

  [self setAuthentication:auth]; 
  //self.auth = auth;   


   if([self isSignedIn]) {

      NextViewController *nextViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
     [self.navigationController pushViewController:nextViewController animated:YES];
   }
}

- (void)setAuthentication:(GTMOAuthAuthentication *)auth {
    mAuth = nil;
    mAuth = [auth retain];
}

在这里你可以通过调用'-canAuthorize'方法检查用户是否被授权。它检查授权并在授权时返回真标志。

(BOOL)isSignedIn
{
   BOOL isSignedIn = self.auth.canAuthorize;
   return isSignedIn;
 }
于 2013-11-30T13:38:58.600 回答