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



我正在尝试获取用户的好友列表。我在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];
}

Facebook在其新版本中更改了许多实现,即V2.0,其中Tag-gable Friends API、Invitable Friends API、Social Context API、Business Mapping API和Tagged Places API是新功能。他们还进行了许多权限更改,以提高安全性。从以下链接中了解更多信息

  • https://developers.facebook.com/docs/apps/changelog#v2_0_permissions

希望您能从以上链接中找到所需的实现更改。

由于自v2.0以来,所有friends_*权限都已被删除,因此无法再接收username字段以及用户好友除public_profile之外的所有信息。

此外,你不会收到完整的朋友列表,只会收到那些同样使用你的应用程序的朋友。

参见

  • https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
  • https://developers.facebook.com/docs/apps/changelog#v2_0_permissions

报价:

  • /me/username不再可用。

  • /me/friends端点不再包括一个人的朋友的完整列表。相反,它现在会返回该人的朋友列表,这些朋友也在使用你的应用程序。

  • 已删除所有friends_*权限。

最新更新