0

I am retrieving facebook friends list by using social framework. Kindly look at my following code that how I am doing but It give me only few friends not all. I checked on two accounts, 1 account has 300 and it returns me only one and one has 50 and it returns me only 3. I also hit next page but that is empty.

  NSArray *accounts = [_accountStore accountsWithAccountType:FBaccountType];
         //it will always be the last object with single sign on
         _facebookAccount = [accounts lastObject];


         ACAccountCredential *facebookCredential = [_facebookAccount credential];
         NSString *accessToken = [facebookCredential oauthToken];
         NSLog(@"Facebook Access Token: %@", accessToken);
         _facebookAccount = [accounts lastObject];
         _facebookToken = [facebookCredential oauthToken];
         //             _facebookToken = _facebookAccount.credential.oauthToken;
         NSLog(@"facebook account =%@",_facebookAccount);
         // Request friend list
         NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name",@"fields", nil];

         SLRequest *friendsListRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                            requestMethod:SLRequestMethodGET
                                                                      URL: [[NSURL alloc] initWithString:@"https://graph.facebook.com/me/friends"] parameters:param];
         friendsListRequest.account = _facebookAccount;
         [friendsListRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

             // Parse response JSON
             NSError *jsonError = nil;
             NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
                                                                  options:NSJSONReadingAllowFragments
                                                                error:&jsonError];
             NSMutableArray * fbFriends=[[NSMutableArray alloc] init];
             for (NSDictionary *friend in data[@"data"]) {

                 NSLog(@"name: %@", friend[@"name"]);
                 [fbFriends addObject:friend];
             }
             [[NSUserDefaults standardUserDefaults] setObject:fbFriends forKey:@"FBFriends"];

         }];

Kindly suggest me where I am doing wrong. Thanks in advance.

josh
  • 1,651
  • 4
  • 27
  • 59

2 Answers2

1
FBRequest* friendsRequest = [FBRequest requestForMyFriends];

[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error)
 {
     NSArray* friends = [result objectForKey:@"data"];
     //NSLog(@"%@",friends);

     [app.Arr_Facebook_Frnd removeAllObjects];

     // CREATE ARRAY OF ALL FRIEND LIST OF FACEBOOK

     for (int i=0; i < [friends count]; i++)
     {
         // ADD IMAGE URL IN ARRAY
         NSString *s1=[NSString stringWithFormat:@"%@",[[friends objectAtIndex:i] objectForKey:@"first_name"]];
         NSString *s2=[NSString stringWithFormat:@"%@",[[friends objectAtIndex:i] objectForKey:@"id"]];
         NSString *s3=[NSString stringWithFormat:@"%@",[[friends objectAtIndex:i] objectForKey:@"last_name"]];
         NSString *s4=[NSString stringWithFormat:@"%@",[[friends objectAtIndex:i] objectForKey:@"name"]];
         NSString *s5=[NSString stringWithFormat:@"%@",[[friends objectAtIndex:i] objectForKey:@"username"]];
         NSString *s6=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?", [[friends objectAtIndex:i] objectForKey:@"id"]];

         NSMutableDictionary *dictFriend=[[NSMutableDictionary alloc]init];
         [dictFriend setObject:s1 forKey:@"first_name"];
         [dictFriend setObject:s2 forKey:@"id"];
         [dictFriend setObject:s3 forKey:@"last_name"];
         [dictFriend setObject:s4 forKey:@"name"];
         [dictFriend setObject:s5 forKey:@"username"];
         [dictFriend setObject:s6 forKey:@"ImgUrl"];

         [app.Arr_Facebook_Frnd addObject:dictFriend];

         [dictFriend release];

     }
     [app.Arr_Facebook_Frnd retain];

     NSSortDescriptor *sortDescriptor;
     sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
     NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
     [app.Arr_Facebook_Frnd sortUsingDescriptors:sortDescriptors];
     [sortDescriptor release];


     [self.indicator stopAnimating];
 }];
Bhavesh Nayi
  • 3,548
  • 1
  • 25
  • 41
  • Thanks Bhavesh, I am using social framework, so kindly guide me according to that because my minimum ios app is iOS 7. – josh Jul 16 '14 at 08:52
1

If your app is not a game this may be the normal behavior.

Have a look to this answer:

Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Community
  • 1
  • 1
tgyhlsb
  • 1,845
  • 11
  • 21
  • Thanks Tanguy, this was really helpful!. – josh Jul 16 '14 at 09:03
  • One more thing, I am getting friends but not all, I think will have to control next ones from paging but how to call next page. Here is my response which I am getting. paging = { cursors = { after = "QWFMNGk0WldKcFNTcGFPU04tMzNzdE9tQTZkUUM2dWhYNjhvTk1VYW9tUTRCWGhBc2oteWU0bUdZY2FpZTR1V1VRWWRfY1BUZ19rUTRjT3FrQkNsbUZFWUp6a3FSekZNd205Q0k1VXdoOWZjcnc="; before = "QWFMdFl2bEw5Rmh3VlVEdHdPcWZXV3QyWmdpM2JIcjFXR0dPMVVCX1dwYWFyajF4SzBFYjk5RFhQSVN6LWwyQWxUZ3doVkdTcnBHdHNOQVctbUkwaFJRWDNLVXZtVGNrZGhSS1ZHa3d4MDc0cFE="; }; }; – josh Jul 16 '14 at 09:06
  • I have no idea for this. But I bet you have to handle it in your `NSDictionary *param`. Or you can do as Bhavesh Nai suggested and use the iOS SDK to make your request. – tgyhlsb Jul 16 '14 at 09:17