iOS 谷歌登录 : Get list of people from Google+ including their names and profile pictures

标签 ios google-plus google-signin google-plus-signin

Google+ 登录现已弃用,Google 建议开发人员改用 Google 登录。使用 GIDSignIn 类,我能够让用户通过他们的 Google 帐户登录。现在我想从 Google+ 获取联系人列表,包括姓名和个人资料图片。这个问题的理想解决方案是什么?

我发现以下链接很有帮助。 http://www.appcoda.com/google-sign-in-how-to/

此外,这篇 StackOverflow 帖子给出了该问题的不完整解决方案。 Get Google contacts using API on iOS

请分享您的解决方案。

最佳答案

在我之前提到的链接的帮助下,我能够在一定程度上解决问题。

首先,我将 AFNetworking 包含到我的 Xcode 项目中。 https://github.com/AFNetworking/AFNetworking

其次,在 AppDelegate.m 中,我在 application:didFinishLaunchingWithOptions: 方法中使用了以下代码。

[[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"]];

然后,在我的 FriendsViewController.m 文件中,我在 Collection View 中显示了 Google+ 中的人员列表,包括姓名和个人资料图片,我使用了以下代码。

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)
    {

    }];
}

不要忘记实现以下链接中提到的其他要点。 https://developers.google.com/identity/sign-in/ios/start-integrating https://developers.google.com/identity/sign-in/ios/sign-in

该解决方案并不完美,因为从 url 加载所有图像时存在延迟。我想获得有关解决方案和可能改进想法的反馈。

关于iOS 谷歌登录 : Get list of people from Google+ including their names and profile pictures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38384892/

相关文章:

android - GoogleApiClient : "Result has already been consumed" in multi-thread Android app

ios - -[_ SwiftValue integerValue]:无法识别的选择器发送到实例0x60000044d560使用Google Mobile Vision时出错

objective-c - 核心图形拖动像素而不是点

google-plus - Google Plus API Moment.Insert

ios - 重新启动应用程序时 GIDSignIn.sharedInstance().currentUser nil

android - 使用谷歌登录 `gms.auth.UserRecoverableAuthException: BadAuthentication` 异常 android

python - 如何在 iOS Appium 上按 "OK"弹出位置

ios - SwiftUI - 如何在我的 View 中调用函数?

user-interface - 如何构建看起来像 Google+ 的 Web 应用程序?

seo - 我可以为 Google+ 商业页面使用 rel=author 吗?