It's actually much easier than than that. You just need another VC to call
GTMOAuth2ViewControllerTouch.
- For a quick demo project: In storyboard, create a single
viewController embedded in a navigation controller. create labels for
whatever data you want to display (i.e. token, exp.date, email, service
provider etc.) and a button to start the sign-in.
- Create a new viewController class and link it to the VC you created
in storyboard. link the IBOutlets you created above. implement the
following action for your start button. (make sure to include the
GTMOAuth2ViewTouch.xib in your project even though you are using a
storyboard) #import "GTMOAuth2ViewControllerTouch.h"
- Implement the callback - (void)viewController:(
GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(
GTMOAuth2Authentication *)auth error:(NSError *)error- at the end of
which update your labels to the returned values
- (IBAction)signinButton:(UIButton *)sender {
GTMOAuth2ViewControllerTouch *authVC = [[GTMOAuth2ViewControllerTouch alloc]
initWithScope:scope
clientID:kMyClientID
clientSecret:kMyClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector
(viewController:finishedWithAuth:error:)];
[self.spinner startAnimating];
[[self navigationController] pushViewController:authVC animated:YES];
}
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {
if (error != nil) {
// Authentication failed (perhaps the user denied access, or closed
the
// window before granting access)
NSLog(@"Authentication error: %@", error);
NSData *responseData = [[error userInfo] objectForKey:@"data"]; //
kGTMHTTPFetcherStatusDataKey
if ([responseData length] > 0) {
// show the body of the server's authentication failure response
NSString *str = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding] ;
NSLog(@"%@", str);
}
self.auth = nil;
} else {
// Authentication succeeded
//
// At this point, we either use the authentication object to
explicitly
// authorize requests, like
//
// [auth authorizeRequest:myNSURLMutableRequest
// completionHandler:^(NSError *error) {
// if (error == nil) {
// // request here has been authorized
// }
// }];
//
// or store the authentication object into a fetcher or a Google
API service
// object like
//
// [fetcher setAuthorizer:auth];
// save the authentication object
self.auth = auth;
NSLog(@"authorization=%@\nexpiration date=%@",self.auth.accessToken,
self.auth.expirationDate);
self.serviceProviderLabel.text=self.auth.serviceProvider;
self.clientEmailLabel.text=self.auth.userEmail;
self.tokenLabel.text=self.auth.accessToken;
self.expireDate.text=[self.auth.expirationDate description];
}
[self.spinner stopAnimating];
//[self updateUI];
}