0

Google+ Sign-In is now deprecated and Google is advising developers to use Google Sign-In instead. Using GIDSignIn class I was able to make users login via their Google account. Now I want to get the contact list including names and profile pictures from Google+. What is the ideal solution to this problem?

I found the following link helpful. http://www.appcoda.com/google-sign-in-how-to/

Also this StackOverflow post gave an incomplete solution to the problem. Get Google contacts using API on iOS

Please share your solutions.

Community
  • 1
  • 1
Ehtesham Hasan
  • 4,133
  • 2
  • 23
  • 31
  • https://stackoverflow.com/questions/40163529/integrate-google-contacts-api-into-my-swift-3-app/54710237#54710237 – Naresh Feb 15 '19 at 13:30

1 Answers1

1

With the help of the links I stated before, I was able to somewhat solve the problem.

First of all, I included AFNetworking into my Xcode project. https://github.com/AFNetworking/AFNetworking

Secondly, in the AppDelegate.m I used the following code in the application:didFinishLaunchingWithOptions: method.

[[GIDSignIn sharedInstance] setClientID:
                @"XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"];
[[GIDSignIn sharedInstance] setShouldFetchBasicProfile:YES]; // default value
[[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.login",
                                            @"https://www.googleapis.com/auth/plus.me"]];

Then in my FriendsViewController.m file where I show the list of people from Google+ including names and profile pictures in a collection view, I used the following code.

if ([[GIDSignIn sharedInstance] hasAuthInKeychain])
{
    NSString * urlString = [NSString stringWithFormat:
        @"https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=%@",
                [GIDSignIn sharedInstance].currentUser.authentication.accessToken];
        // use connected in place of visible if you want only the people who use the app

    AFJSONResponseSerializer * responseSerializer =
        [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

    AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
    [sessionManager setResponseSerializer:responseSerializer];
    [sessionManager GET:urlString parameters:nil progress:nil
            success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
    {       
        if (responseObject != nil)
        {
            NSArray * arrayFriends = [responseObject valueForKey:@"items"];

            for (id object in arrayFriends)
            {
                NSString * stringName = [object valueForKey:@"displayName"];

                NSString * stringUrlProfilePicture =
                        [[object valueForKey:@"image"] valueForKey:@"url"];
                NSURL * urlProfilePicture = [NSURL URLWithString:stringUrlProfilePicture];
                NSData * dataProfilePicture = [NSData dataWithContentsOfURL:urlProfilePicture];
                UIImage * imageProfilePicture = [UIImage imageWithData:dataProfilePicture];

                NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
                [dictionary setObject:stringName forKey:@"name"];
                [dictionary setObject:imageProfilePicture forKey:@"profilePicture"];

                [self.arrayDictionaryFriends addObject:dictionary]; 
                    // self.arrayDictionaryFriends is used as the data source
            }

            [self.collectionViewFriends reloadData];
        }
    }
            failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {

    }];
}

Don't forget to implement the other points mentioned in the following links. https://developers.google.com/identity/sign-in/ios/start-integrating https://developers.google.com/identity/sign-in/ios/sign-in

The solution is not perfect as there is a delay when all the images are loaded from the urls. I would like to get feedback on the solution and idea of possible improvements.

Ehtesham Hasan
  • 4,133
  • 2
  • 23
  • 31