简体   繁体   English

如何使用Facebook iOS SDK 4.0获取包含用户名和ID的用户朋友列表

[英]How get User's Friends list that contain Username and ids using Facebook iOS SDK 4.0

I'm trying to Fetch User's friends List. 我正在尝试获取用户的朋友列表。 my last working Code that i implement in FacebookSDK 3.12 is now of No use because facebook have changed all their classes and even deleted old Classes, below is my old Code: 我在FacebookSDK 3.12中实现的最后一个工作代码现在已无用,因为Facebook更改了所有类,甚至删除了旧类,以下是我的旧代码:

#pragma mark - Facebook Methods -
-(void)initiateFacebookSessionIfNot
{
    if (FBSession.activeSession.isOpen)
    {
        [self getUserFriendsIds];
    } else
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_birthday",@"friends_hometown",
                                @"friends_birthday",@"friends_location",
                                nil];
        [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status,NSError *error)
         {
             if (error) {
                 NSString *alertMessage, *alertTitle;

                 if (error.fberrorShouldNotifyUser) {
                     alertTitle = @"Something Went Wrong";
                     alertMessage = error.fberrorUserMessage;
                 } else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
                     NSLog(@"user cancelled login");
                 } else {
                     // For simplicity, this sample treats other errors blindly.
                     alertTitle  = @"Unknown Error";
                     alertMessage = @"Error. Please try again later.";
                     NSLog(@"Unexpected error:%@", error);
                 }

                 if (alertMessage)
                 {
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
                                                                     message:error.fberrorUserMessage
                                                                    delegate:nil
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil];
                     [alert show];
                 }
             } else if (FB_ISSESSIONOPENWITHSTATE(status)) {
                 [self getUserFriendsIds];
             }
         }];
    }
}
-(void)getUserFriendsIds
{
    FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,username"];
    [ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSMutableArray *fbFriendIDs;
        NSMutableArray *fbFriendsName;
        NSMutableArray *fbFriendsUserName;

        NSMutableArray *data = [[NSMutableArray alloc]initWithArray:[result objectForKey:@"data"]];
        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if (!(NSDictionary*)obj[@"username"]) {

                NSDictionary *newDictionary =@{@"id": (NSDictionary*)obj[@"id"],
                                               @"name": (NSDictionary*)obj[@"name"],
                                               @"username": @"0"
                                               };
                [data replaceObjectAtIndex:idx withObject:newDictionary];
            }
        }];

        fbFriendsUserName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.username"]];
        fbFriendsName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.name"]];
        fbFriendIDs = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.id"]];

        NSDictionary *queryDictionary =
        @{@"user_id": userObj.user_id,
          @"friend_ids":[fbFriendIDs componentsJoinedByString:@","],
          @"names":[fbFriendsName componentsJoinedByString:@","],
          @"usernames":[fbFriendsUserName componentsJoinedByString:@","]
          };

        [[RequestHandler sharedClient] CheckFBFriendsWithDictionary:queryDictionary WithCompletionBlock:^(NSArray *TUNFriendsArray, NSArray *FBFriendsArray, NSString *errorMessage) {
            if (!errorMessage) {

                tunFriendsArray = [[NSMutableArray alloc]initWithArray:TUNFriendsArray];
//                fbFriendsArray = [[NSMutableArray alloc]initWithArray:FBFriendsArray];

                [_facebookTableView reloadData];
            }
            else{
                [ApplicationUtilities ShowAlertViewWithTitle:APP_NAME Message:errorMessage];
            }
        }];
    }];
    [SVProgressHUD dismiss];
}

It's no longer possible to receive the username field, as well as every information of the user's friends other than the public_profile, because all the friends_* permissions have been removed since v2.0. 从v2.0开始,所有的friends_*权限都已删除,因此不再可能接收username名字段以及除public_profile之外的用户朋友的所有信息。

Furthermore, you don't receive the full friends list, but only those which also use your app. 此外,您不会收到完整的朋友列表,而只会收到也使用您的应用程序的朋友列表。

See 看到

Quotes: 行情:

  • /me/username is no longer available. /me/username不再可用。

  • The /me/friends endpoint no longer includes the full list of a person's friends. /me/friends端点不再包括一个人的朋友的完整列表。 Instead, it now returns the list of that person's friends who are also using your app. 相反,它现在返回也在使用您的应用程序的该人的朋友列表。

  • All friends_* permissions have been removed. 所有friends_*权限均已删除。

Facebook has changed a lot of implementation in their new version ie V2.0 in which Tag-gable Friends API , Invitable Friend API , Social Context API , Business Mapping API and Tagged Places API are the new features. Facebook在其新版本(即V2.0)中更改了许多实现,其中的新功能包括Tag-gable Friends APIInvitable Friend APISocial Context APIBusiness Mapping APITagged Places API They have also made many permission changes so as to increase security read more on this from the following link 他们还进行了许多权限更改,以提高安全性,请从以下链接中阅读更多内容。

Hope you will find your required implementation changes from the above links. 希望您可以从上述链接中找到所需的实现更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM