Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Google Sign-in fetching user profile picture impossible?(NOT Google plus sign-in)

I'm trying to make google sign-in work in my app but have an issue.

(NOT a google plus sign-in, I'm using google sign-in)

I followed this link and it works. I get userID, user.authentication.idToken, user.profile.name, and user.profile.email.

But I can't find out the way how I get the user profile picture. Above google docs have no comment on that.

I searched web for hours but found about google plus sign-in only which I don't want.

I implemented Facebook sign-in in my app and they provide the url that fetches user profile picture.

Does google sign-in serve something like that?

This is my working code fetching user info but no picture.

Please help

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {

if (user) {
    NSString *userId = user.userID;                  // For client-side use only!
    NSString *token = user.authentication.idToken;   // Safe to send to the server
    NSString *name = user.profile.name;
    NSString *email = user.profile.email;
...
}
like image 210
Vincent Gigandet Avatar asked Jul 29 '15 18:07

Vincent Gigandet


2 Answers

you have to set [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES; and the use below code for get profile pic

    if ([GIDSignIn sharedInstance].currentUser.profile.hasImage)
    {
        NSUInteger dimension = round(thumbSize.width * [[UIScreen mainScreen] scale]);
        NSURL *imageURL = [user.profile imageURLWithDimension:dimension];
    }

Swift Implementation:

let dimension = round(thumbSize.width * UIScreen.mainScreen().scale);
let pic = user.profile.imageURLWithDimension(dimension) 
like image 59
Amit Avatar answered Sep 28 '22 06:09

Amit


Swift 3

let dimension = round(imageSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)

imageSize.width is required width of image.

let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)

Thanks Amit

like image 24
Sourabh Sharma Avatar answered Sep 28 '22 06:09

Sourabh Sharma