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.