Ashit Soni
Ashit Soni

Reputation: 226

Can't get Facebook friends with FBFriendPicker in iOS

I want to get Facebook friends in my project. I have tried the FBFriendPicker Method, but I can't get the friends. It shows empty list to me.

Here is my code.

- (IBAction)pickFriendsButtonClick:(id)sender {

if (!FBSession.activeSession.isOpen) {
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState state,
                                                      NSError *error) {
                                      if (error) {
                                          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                              message:error.localizedDescription
                                                                                             delegate:nil
                                                                                    cancelButtonTitle:@"OK"
                                                                                    otherButtonTitles:nil];
                                          [alertView show];
                                      } else if (session.isOpen) {
                                          [self pickFriendsButtonClick:sender];
                                      }
                                  }];
    return;
}

if (self.friendPickerController == nil) {
    // Create friend picker, and get data loaded into it.
    self.friendPickerController = [[FBFriendPickerViewController alloc] init];
    self.friendPickerController.title = @"Pick Friends";
    self.friendPickerController.delegate = self;
}

[self.friendPickerController loadData];
[self.friendPickerController clearSelection];

[self presentModalViewController:self.friendPickerController animated:YES];
}

Upvotes: 1

Views: 206

Answers (4)

Rakesh Thummar
Rakesh Thummar

Reputation: 197

Try this code for get facebook friends....

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
friendsRequest.session = FBSession.activeSession;

[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 %@ ", friend.name);
    }
}];

Upvotes: 1

Sahil Mittal
Sahil Mittal

Reputation: 20753

With the new API version v2.0, you cannot get the complete list of friends just with user_friends. Your picker must be using /me/friends which will give you the list of friends using that app. So in your case none of your friends be using this app and that's why you are getting a blank response.

To get the complete list of friends you should use /me/taggable_friends API. But note that this permission needs to be reviewed first by facebook before you go live. See this answer for more info for using this permission.

Upvotes: 0

Rohit
Rohit

Reputation: 577

Try This...

-(IBAction)btnFacebookClick:(id)sender
{
    NSArray *permissions = [[NSArray alloc] initWithObjects:                            @"user_about_me,user_birthday,user_hometown,user_location,email",@"read_mailbox",@"read_stream",nil];


    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState status,NSError *error)
     {
         if(error)
         {
             NSLog(@"session error %@",error);

         }
         else if(FB_ISSESSIONOPENWITHSTATE(status))
         {

             [self getFriendList];
         }


     }];
}


-(void)getFriendList
{
    FBRequest *friendsRequest=[FBRequest requestForMyFriends];


    [friendsRequest startWithCompletionHandler:^(FBRequestConnection *connection,NSDictionary* result,NSError *error)
     {
         friendsArr = [result objectForKey:@"data"];

         NSLog(@"friends description :%@",[friendsArr description]);

     }];
}

Upvotes: 1

Rupal Patel
Rupal Patel

Reputation: 570

Please try below code :

[FBSession openActiveSessionWithReadPermissions:@[@"email"]
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                  if (error) {
                                      // Handle errors
                                      [self handleAuthError:error];
                                  }
                                  else
                                  {
                                      // Check for publish permissions
                                      FBRequest *req = [FBRequest requestForMyFriends];
                                      [req startWithCompletionHandler:^(FBRequestConnection *connection,NSDictionary *result,NSError *error){
                                          NSLog(@"Result:- %@",[result objectForKey:@"data"]);
                                      }];
                                  }
                              }];

Upvotes: 0

Related Questions