The GPPSignIn class has a trySilentAuthentication
method that attempts to automatically sign in the user. This call succeeds if the user has authorized your application in the past, they haven't revoked access to your application, and the app isn't requesting new scopes since they last signed in. If this call succeeds, it calls yourfinishedWithAuth:error:
method when sign in is complete.
Add the [signIn trySilentAuthentication]
call to the end of your viewDidLoad
method.
Final step: To get Logged user Profile info
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error) {
self.SigninAuthStatus.text =
[NSString stringWithFormat:@"Status: Authentication error: %@", error];
return;
}
// getting the access token from auth
NSString *accessTocken = [auth valueForKey:@"accessToken"]; // access tocken pass in .pch file
[accessTocken retain];
NSLog(@"%@",accessTocken);
NSString *str=[NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];
NSString *escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:[jsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
// NSMutableDictionary *proDic = [[NSMutableDictionary alloc] init];
NSString *userId=[jsonDictionary objectForKey:@"id"];
// proDic=[jsonData JSONValue];
NSLog(@" user deata %@",jsonData);
NSLog(@"Received Access Token:%@",auth);
// NSLog(@"user google user id %@",signIn.userEmail); //logged in user's email id
//
GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease];
plusService.retryEnabled = YES;
[plusService setAuthorizer:auth];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:userId];
[plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,GTLPlusPerson *person,NSError *error) {
if (error) {
GTMLoggerError(@"Error: %@", error);
} else {
// Retrieve the display name and "about me" text
[person retain];
NSString *description = [NSString stringWithFormat:
@"%@\n%@", person.displayName,
person.aboutMe];
GTLPlusPersonImage *image =person.image;
NSString *strimag=[image valueForKey:@"url"];
// [self setImageFromURL:[NSURL URLWithString:strimag]];
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strimag]];
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
receivedData=UIImageJPEGRepresentation(img,50);
UIImage *img1=[UIImage imageWithData:receivedData];
self.imageView.image = img1;
// UIImage *image=[UIImage]
// self.imageView.image=imgage;
NSLog(@"hai in person Image %@",image);
NSLog(@"description %@",description);
}
}];
// [self reportAuthStatus];
[[GPPSignIn sharedInstance] signOut];
}