Facebook iOS SDK 3.0 - save and reload .selection

2019-02-18 04:53发布

问题:

FBFriendPickerViewController seems pretty straightforward. However, it may not be designed for the scenario I want.

Essentially, I want to save a list of friends the user selects to disk. On subsequent app launches, I want to show the FBFriendPickerViewController with these "saved" friends as being already selected.

Since the .selection attribute is readonly, I'm not sure how I can load a new instance of FBFriendPickerViewController with a subset of friends already "selected".

回答1:

Finally I've found out the solution.

I have an NSDictionary dict whose key is the facebook user id and the value is the name. Roughly, this is the code:

NSMutableArray *results = [[NSMutableArray alloc] init];
for (id key in dict) {
  NSString *name = [dict valueForKey:key];
  id<FBGraphUser> user = (id<FBGraphUser>)[FBGraphObject graphObject];
  user.id = key;
  [user setName:name]; // This is not mandatory
  if (user) {
    NSLog(@"adding user: %@", user.name);
    [results addObject:user];
  }
}
// And finally set the selection property
friendPickerController.selection = results;

UPDATE: it seems like it doesn't work properly....you will see the friends correctly checked, but when you will go to parse the .selection when you finish the selection, you will get only the friends you checked in the UI, not the preloaded ones...

UPDATE 2: it seems to work fine if you fill up all the fields listed in the FBGraphUser.h header file