Search code examples
facebook-ios-sdk

Getting number of friends in FBFriendPickerViewController


I am using FBFriendPickerViewController to display a list of friends and I'm trying to find an easy way to get the number of friends in the list. I've tried implementing the friendPickerViewControllerDataDidChange:(FBFriendPickerViewController *)friendPicker method but it sometimes (especially while testing on different devices such as 3.5 inch iPhone vs 4.0 inch iPhone) gets fired twice (definitely a bug) and messes up the number. Is there any reliable way to get the number of friends like [friendPicker numberOfFriends]?

Thanks, Can.


Solution

  • I Don't really like using the tableview to fetch the number of friends, you can simply do that with a graph call, but if you want to use the view, a simple way is to do it as follows:

        int sum = 0;
        for (int x=0; x< [friendPickerController.tableView numberOfSections]; x++) {
            sum+= [friendPickerController.tableView numberOfRowsInSection:x];
        }
        NSLog(@"You have %d friends", sum);
    

    Again, not the best way to do it, but if that's what you need, there it is. also you need to make sure the data is already loaded before trying to count the rows, else the sum will be 0

    To know when the data has been loaded, create a FBFriendPickerDelegate and use the method:

    - (void)friendPickerViewControllerDataDidChange:(FBFriendPickerViewController *)friendPicker;
    

    To know when the data has been loaded.

    Here's Facebook's reference

    Cheers !