Vamos Vamos - 6 months ago 54
Objective-C Question

How to fetch profile picture from Google + in iOS?

Currently in my app i am fetching Email, GoogleID, User Name, Gender from Google+.

Here is my code in viewDidLoad

GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:
kGTLAuthScopePlusMe,
kGTLAuthScopePlusUserinfoEmail,
kGTLAuthScopePlusLogin ,
nil];

signIn.shouldFetchGoogleUserID = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.delegate = self;
[signIn trySilentAuthentication];


And here is my GPPSignInDelegate method

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{


if (error)
{

} else
{
[self refreshInterfaceBasedOnSignIn];

GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];

NSLog(@"email %@ ", [NSString stringWithFormat:@"Email: %@",[GPPSignIn sharedInstance].authentication.userEmail]);
NSLog(@"Received error %@ and auth object %@",error, auth);


GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
plusService.retryEnabled = YES;


[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];


[plusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLPlusPerson *person,
NSError *error) {
if (error)
{

} else
{
NSLog(@"Email= %@", [GPPSignIn sharedInstance].authentication.userEmail);
NSLog(@"GoogleID=%@", person.identifier);
NSLog(@"User Name=%@", [person.name.givenName stringByAppendingFormat:@" %@", person.name.familyName]);
NSLog(@"Gender=%@", person.gender);
}
}];
}


}

and

-(void)refreshInterfaceBasedOnSignIn
{

if ([[GPPSignIn sharedInstance] authentication])
{

self.signInButton.hidden = YES;

} else
{
self.signInButton.hidden = NO;

}


}

How can i fetch profile picture URL from Google+?
I referred this Getting Google+ profile picture url with user_id but i didnt received accurate answer.

Answer

try this

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];

here I add full code

GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID=@GOOGLE_PLUS_Login_ID;  // here add your ID
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ @"profile" ];

signIn.delegate = self;

if (![signIn trySilentAuthentication])    // if it is not authenticate it re authenticate once
    [signIn authenticate];


 -(void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error {
if (error) {
    // Do some error handling here.
} else {
      // NSLog(@"Received error %@", auth.userEmail);
      // NSLog(@"Received error %@", auth.accessToken);

    if ( auth.userEmail)
    {
        [[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error)
         {
 // this is for fetch profile image
 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
             NSLog(@"%@",url);
NSLog(@"Name:%@",person.displayName);
  }];

      }

 }
}