2014-09-29 91 views
1

我正在使用iOS sdk v3.18.1,我想要获得我所有的Facebook好友。我可以让朋友数,但数据为零。如何从Facebook获取好友列表iOS sdk

这里是我的代码

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

出把

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

检查这个http://stackoverflow.com/questions/6638955/facebook-ios-sdk-get-friends-list – hariszaman 2014-09-29 09:07:55

+0

尝试/我/ friendlists – 2014-09-29 09:12:46

+0

/me/friendlist是错误的,它让你的名单,但不是朋友 – luschn 2014-09-29 09:13:20

回答

0

,因为图形API的V2.0,你将只能得到谁是与你的应用程序连接的好友列表。在Graph API的v2.0中,调用/ me/friends会返回使用该应用程序的人的朋友。是的,有可能获得计数,但访问朋友列表是不可能的。

4月份之后所有Facebook SDK的发布都否定了获取整个朋友列表的功能。

REFER:SO QUESTION:Facebook graph API returns empty .... FACEBOOK USER GUIDE

This has been confirmed by FACEBOOK

2
// declare an array in header file which will hold the list of all friends - 
NSMutableArray * m_allFriends; 

// alloc and initialize the array only once 
m_allFriends = [[NSMutableArray alloc] init]; 

随着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的限制)并将其传递给param。

//////////////////////////////////////////////////////////////////////// 

对于非应用程序的朋友 -

// 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]; 
} 

邀请和非应用的朋友 -

您将收到我/ invitable_friends图形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); 
       } 
      } 
     } 
    }]; 
} 
相关问题