2014-09-29 100 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); 
       } 
      } 
     } 
    }]; 
} 
相關問題