简体   繁体   中英

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

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]);

     }];
}

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

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"]);
                                      }];
                                  }
                              }];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM