Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Facebook iOS sdk获取好友列表_Ios_Objective C_Iphone_Facebook Graph Api - Fatal编程技术网

如何从Facebook iOS sdk获取好友列表

如何从Facebook iOS sdk获取好友列表,ios,objective-c,iphone,facebook-graph-api,Ios,Objective C,Iphone,Facebook Graph Api,我使用的是iOS sdk v3.18.1,我想获取所有Facebook好友。我可以获取好友数,但数据为零 这是我的密码 [FBRequestConnection startWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSLog(@"resul

我使用的是iOS sdk v3.18.1,我想获取所有Facebook好友。我可以获取好友数,但数据为零

这是我的密码

[FBRequestConnection startWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSLog(@"result %@",result);
    }];
输出

{
    data =     (
    );
    summary =     {
        "total_count" = 840;
    };
}

由于v2.0版您无法再获得完整的好友列表,因此您也只能获得授权您应用程序的好友


在这个帖子中也可以看到我的答案:

因为Graph API的V2.0版,你只能获得与你的应用程序连接的朋友列表。在Graph API的v2.0中,调用/me/friends返回使用该应用程序的人的朋友。是的,可以获取计数,但无法访问好友列表

4月份之后发布的所有Facebook SDK都否认了获取全部好友列表的功能

参考:

使用FB SDK 3.0和2.0以上的API版本,您需要调用下面的函数(与我/朋友一起使用图形API),以获取使用相同应用程序的FB朋友列表

// get friends which use the app

-(void) getMineFriends
{
    [FBRequestConnection startWithGraphPath:@"me/friends"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              NSLog(@"me/friends result=%@",result);

                              NSLog(@"me/friends error = %@", error.description);

                              NSArray *friendList = [result objectForKey:@"data"];

                              [m_allFriends addObjectsFromArray: friendList];
                          }];
}
注:1)上述查询返回的好友数默认限制为25。2) 如果下一个链接出现在结果中,这意味着在下一个查询中,您将获取更多的朋友,以此类推。3) 或者,您可以更改限制(减少限制,从25超出限制),并在参数中传递该限制

////////////////////////////////////////////////////////////////////////
对于非应用程序好友-

// m_invitableFriends - global array which will hold the list of invitable friends
另外,要获得非应用程序好友,您需要使用(/me/invitable_friends),如下所示-

- (void) getAllInvitableFriends
{
    NSMutableArray *tempFriendsList =  [[NSMutableArray alloc] init];
    NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:@"100", @"limit", nil];
    [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}

- (void) getAllInvitableFriendsFromFB:(NSDictionary*)parameters
                            addInList:(NSMutableArray *)tempFriendsList
{
    [FBRequestConnection startWithGraphPath:@"/me/invitable_friends"
                                 parameters:parameters
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              NSLog(@"error=%@",error);

                              NSLog(@"result=%@",result);

                              NSArray *friendArray = [result objectForKey:@"data"];

                              [tempFriendsList addObjectsFromArray:friendArray];

                              NSDictionary *paging = [result objectForKey:@"paging"];
                              NSString *next = nil;
                              next = [paging objectForKey:@"next"];
                              if(next != nil)
                              {
                                  NSDictionary *cursor = [paging objectForKey:@"cursors"];
                                  NSString *after = [cursor objectForKey:@"after"];
                                  //NSString *before = [cursor objectForKey:@"before"];
                                  NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:
                                                              @"100", @"limit", after, @"after"
                                                              , nil
                                                              ];
                                  [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
                              }
                              else
                              {
                                  [self replaceGlobalListWithRecentData:tempFriendsList];
                              }
                          }];
}

- (void) replaceGlobalListWithRecentData:(NSMutableArray *)tempFriendsList
{
    // replace global from received list
    [m_invitableFriends removeAllObjects];
    [m_invitableFriends addObjectsFromArray:tempFriendsList];
    //NSLog(@"friendsList = %d", [m_invitableFriends count]);
    [tempFriendsList release];
}
邀请非应用程序好友-

您将通过me/invitable_friends graph api返回的好友列表获得邀请令牌。您可以在FBWebDialogs中使用这些邀请令牌向朋友发送邀请,如下所示

- (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   userInviteTokens, @"to",
                                   nil, @"object_id",
                                   @"send", @"action_type",
                                   actionLinksStr, @"actions",
                                   nil];

    [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Hi friend, I am playing game. Come and play this awesome game with me."
     title:nil
     parameters:params
     handler:^(
               FBWebDialogResult result,
               NSURL *url,
               NSError *error)
     {
         if (error) {
             // Error launching the dialog or sending the request.
             NSLog(@"Error sending request : %@", error.description);
         }
         else
         {
             if (result == FBWebDialogResultDialogNotCompleted)
             {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
                 NSLog(@"Friend post dialog not complete, error: %@", error.description);
             }
             else
             {
                 NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];

                 if (![resultParams valueForKey:@"request"])
                 {
                     // User clicked the Cancel button
                     NSLog(@"User canceled request.");
                 }
                 else
                 {
                     NSString *requestID = [resultParams valueForKey:@"request"];

                     // here you will get the fb id of the friend you invited,
                     // you can use this id to reward the sender when receiver accepts the request

                     NSLog(@"Feed post ID: %@", requestID);
                     NSLog(@"Friend post dialog complete: %@", url);
                 }
             }
         }
     }];
}

检查这个/me/friendlist是否错误,它会为您提供列表,但不会为您提供朋友
- (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   userInviteTokens, @"to",
                                   nil, @"object_id",
                                   @"send", @"action_type",
                                   actionLinksStr, @"actions",
                                   nil];

    [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Hi friend, I am playing game. Come and play this awesome game with me."
     title:nil
     parameters:params
     handler:^(
               FBWebDialogResult result,
               NSURL *url,
               NSError *error)
     {
         if (error) {
             // Error launching the dialog or sending the request.
             NSLog(@"Error sending request : %@", error.description);
         }
         else
         {
             if (result == FBWebDialogResultDialogNotCompleted)
             {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
                 NSLog(@"Friend post dialog not complete, error: %@", error.description);
             }
             else
             {
                 NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];

                 if (![resultParams valueForKey:@"request"])
                 {
                     // User clicked the Cancel button
                     NSLog(@"User canceled request.");
                 }
                 else
                 {
                     NSString *requestID = [resultParams valueForKey:@"request"];

                     // here you will get the fb id of the friend you invited,
                     // you can use this id to reward the sender when receiver accepts the request

                     NSLog(@"Feed post ID: %@", requestID);
                     NSLog(@"Friend post dialog complete: %@", url);
                 }
             }
         }
     }];
}