How to get profile picture from Google + in iOS?
Currently in my application I am getting email, GoogleID, username, 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 method GPPSignInDelegate
:
- (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;
}
}
My question is, how can I get the image url from Google+? I have referenced this Get Google+ profile url with user_id , but I didn’t get an exact answer.
+3
source to share
3 answers
try it
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
here i add complete 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);
}];
}
}
}
+3
source to share
How about this?
let user: GIDGoogleUser = ...
if user.profile.hasImage{
let profilePicURL = user.profile.imageURLWithDimension(200).absoluteString
print(profilePicURL)
}
Refer to the description of the GIDProfileData class .
+2
source to share
Swift 3
let dimension = round(thumbSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
thumbSize.width requires the width of the image.
let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
+1
source to share