如何在ios中获取facebook好友列表?

时间:2017-07-07 09:23:13

标签: ios objective-c facebook-graph-api

我在过去两天尝试这个但没有得到任何解决方案。 请帮我。我想获取facebook朋友的朋友列表,我正在使用此代码

loginButton.readPermissions =
    @[@"public_profile", @"email", @"user_friends", @"read_custom_friendlists"];

if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);
             }
         }];

         FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:@"/me/taggable_friends"
                              parameters:@{@"fields": @"id, name"}
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    NSLog(@"%@",result);
}];


    }



   but in the response i am getting :

fetched user:{
    id = 102045934964446131;
    name = "xyz zyz";
}
      id = "AaKwxUJHqY8miA_dKdOIEoDc1NDF4nTw_K4s-JKAYvFqDdyXYe_dsOyDiUuKPh285CNlc5pAvtHhD7K9PbrpfP8pZmsYwujUenNjdfrUOkrm8g";
        name = "Anshul Katiyar";
    },
            {
        id = "AaLx7VNhGwdmA0vyZjy-Ry7II2kEVeQweLp54HiJC2TU5yIN6Qkl-aT811aqm0aea6pjRy-amUTz3gEXanjuQIKvHXiu-cICJ-VXjnchyL1kug";
        name = "Madhu Verma";

    },
            {
        id = "AaLPkAwYvi6aFu8uTgLfTc0QAbS1tlSM600NF07QOezuwEibLjp8Tq2N7TFKuUrXrwsTmAY5bFhtxEg6XmcVJMZ1xLhrQiMR_hNasPsCpiPpYQ";
        name = "Manojsharma Manojsharma";
    },
            {
        id = "AaIyxf6jtQT_c05h3JdJ18pWNYwRYREKf2A5hIiz9qdp-ehXbvYkwMgTbqF4RptgiyxgdUObU8tLcKDTt2JNpPOWFrXty00OsbMqBrK_Up0Xpg";
        name = "Aarti Tripathi";
    },
            {
        id = "AaJPoNnjzzIT6ELXyl56NTpkGx7xtT5J1wf_WOsX4BD4AFvDTcAOt0sn9m51VaJhSTDwC7eUkU7Gw5hPBHyZn8YxsD9HpwgGnZzxnE7yN853jA";
        name = "Vaibhaw Singh";
    },
            {
        id = AaJE5GA9QFr7oD8KY3Gak4h5aJqrHHHh4WYWKWCvpg97V2HSDm8UjSuygfMMg08QIDPWe20qRqY4eP3R9N82AhJ9Lzg1TpjzNdDZsJoy1hWrmg;
        name = "Saurabh Verma";
    },
            {
        id = "AaKGTx-vlLOyuNExGrlcqoFpwQRMtAHqwtCeuslQfriLeN8pDyB0D_qQeIpIULSIcwVB2fGltbOkMW9Mp94AzX2Y2egxgJw0lAHRCE45DJlkIg";
        name = "Udit Katiyar Nitiya";
    }
);
paging =     {
    cursors =         {
        after = QWFMRWdjYUM1aVlpYW00U2t6Ym5oUnpqdW0tdm1xZA2RSQTc3dk16N1d3WnNEaG81Q2V1ekNTRHNCSEtjYjVPam1XbGZAVRGE0UGsxSEdxZADJZAZAkkwX3BHYlZAodGFBQmNPUmlYWTRfNUtkeVVLUmcZD;
        before = QWFJLXktdTVqeEFOR05ueHRWSWE4eWs0WE5nQVJUN0o5eC03YWVpcW10YkRObjMtNmZAXeTRXTDh5WUNrLV9tQnZAiWWdOSFNrVk8wZATl5a3ZAYWVpsRUxDVnpORG5POG1jZAlpIbm5DS0FFcHZAxb3cZD;
    };
    next = "https://graph.facebook.com/v2.9/10204593496146131/taggable_friends?access_token=EAAasBqLeUy0BAIRjZCZBlRXD4Hleg3J8712Drm5BZA6DC3bvLZCcPy6iklwPi77ymOoj888GjZBhyWdZAyI4nK1hSr2D1ZAvpTjYrc4L2cVZAVuYoeRe39mxVPczuH5GZBHHngGIoTAPv5AdnXPjsybJlt2t4fAmRFGaw8ZC1GiJHLtZCsbYEyvsKYCpGCaWJNNVOFcwUZBaKwM5xZB9DoJw7wFzL&fields=id%2C+name&limit=25&after=QWFMRWdjYUM1aVlpYW00U2t6Ym5oUnpqdW0tdm1xZA2RSQTc3dk16N1d3WnNEaG81Q2V1ekNTRHNCSEtjYjVPam1XbGZAVRGE0UGsxSEdxZADJZAZAkkwX3BHYlZAodGFBQmNPUmlYWTRfNUtkeVVLUmcZD";
};

我收到此输出但这不是我想要完整列表如何获取该列表的朋友的完整列表。 我想得到所有朋友的电子邮件,姓名和身份证明。 我有267的朋友,但没有显示朋友的任何细节。  请帮忙。

提前致谢

2 个答案:

答案 0 :(得分:0)

这是我从Facebook获取朋友的工作代码。希望它会对你有所帮助。

-(void)getFacebookFriend
{
    if (FBSession.activeSession.isOpen)
    {
           [self ListFriend];
    }
    else
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",@"user_friends",@"public_profile", nil];

        [FBSession openActiveSessionWithReadPermissions:permissions
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session,
           FBSessionState state, NSError *error) {
             if(!error)
             {

                 [SVProgressHUD showWithStatus:PLEASE_WAIT];
                 [self performSelector:@selector(ListFriend) withObject:nil afterDelay:0.2];
             }
             else
                 [SVProgressHUD dismiss];
         }];
    }
}
-(void)ListFriend
{

    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setObject:@"id, first_name, last_name, middle_name, name, email, picture" forKey:@"fields"];
    [params setObject:[NSString stringWithFormat:@"500"] forKey:@"limit"];
    if(_strPageToken.length > 0)
    {
        [params setObject:_strPageToken forKey:@"paging_token"];
        [params setObject:_strUntill forKey:@"until"];

    }

    [[FBRequest requestWithGraphPath:@"me/taggable_friends" parameters:params HTTPMethod:nil] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
     {
         NSArray *aryFriend = result[@"data"];
         [_aryContact addObjectsFromArray:aryFriend];
         NSLog(@"%lu",(unsigned long)aryFriend.count);
         NSMutableDictionary *dicPaging = result[@"paging"];

         if(dicPaging[@"next"])
         {

             _strNextURL = [NSString stringWithFormat:@"%@",dicPaging[@"next"]];
         }
         else
         {
             NSLog(@"Final Array count:    %lu",(unsigned long)_aryContact.count);
         }
     }];
}

答案 1 :(得分:0)

获取权限:

 NSArray *permissions = @[@"user_birthday", @"friends_hometown",
                        @"friends_birthday", @"friends_location"];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler: .....
NSLog(@"permissions::%@",FBSession.activeSession.permissions);

发布FBRequest:

FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,picture,birthday,location"];
 [ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    NSArray *data = [result objectForKey:@"data"];
    for (FBGraphObject<FBGraphUser> *friend in data) {
        NSLog(@"%@:%@", [friend name],[friend birthday]);
 }}];

此外,您可以在SDK 3.0上试用requestForMyFriends。注意:它可能提供生日信息

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                  NSDictionary* result,
                                  NSError *error) {
    NSArray* friends = [result objectForKey:@"data"];
    NSLog(@"Found: %lu friends", (unsigned long)friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.objectID);
    }
}];