iOS Google登录:获取Google+中的人员列表,包括他们的姓名和个人资料照片

时间:2016-07-14 21:54:54

标签: 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

请分享您的解决方案。

1 个答案:

答案 0 :(得分:1)

在我之前说过的链接的帮助下,我能够在某种程度上解决问题。

首先,我将AFNetworking纳入了我的Xcode项目。 https://github.com/AFNetworking/AFNetworking

其次,在AppDelegate.m中,我在应用程序中使用了以下代码: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"]];

然后在我的FriendsViewController.m文件中,我显示了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

解决方案并不完美,因为从网址加载所有图像时会出现延迟。我希望获得有关可能改进的解决方案和想法的反馈。