如何使用Parse在User中查询Facebook好友?

时间:2015-02-13 16:35:42

标签: ios facebook parse-platform

我使用Parse作为我的数据库。我想访问_Users类,看看我的Facebook好友是否正在使用该应用程序(在类_User中)。我刚看了this,发现你不能再保存好友列表了。

到目前为止我的方式是:

我首先得到朋友的FB ID并在_User中查询。但是,我注意到FacebookID被保存为authData。除了创建一个将facebookID复制为字符串的新列之外,如何使用FacebookID查询我的_User类?

// Send request to Facebook
    [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, id result, NSError *error) {

        [FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (!error) {
                // result will contain an array with your user's friends in the "data" key
                NSArray *friendObjects = [result objectForKey:@"data"];
                NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
                // Create a list of friends' Facebook IDs
                for (NSDictionary *friendObject in friendObjects) {
                    [friendIds addObject:[friendObject objectForKey:@"id"]];
                }
                self.fbfriendList = [NSMutableArray arrayWithArray:friendObjects];

                    PFQuery *friendQuery = [ParseUser query];

//PROBLEM is here where the fbId in my _User class is saved as authData
                    [friendQuery whereKey:@"fbID" containedIn:friendIds];

                    NSArray *friendUsers = [friendQuery findObjects];
                self.fbfriendList = friendObjects;  
            }
        }]; 
    }];

1 个答案:

答案 0 :(得分:2)

解析用户' Facebook ID默认是私有的,但您可以从其他用户访问它们。

要实现此目的,您可以使用Facebook API获取用户的Facebook ID,将其分配给Parse用户的字段。然后,您可以在User集合上构建查询,以获取其Facebook ID在您的用户的朋友列表中的Parse用户列表。

当您使用Facebook登录时,您应该这样做:

// When your user logs in, immediately get and store its Facebook ID
[PFFacebookUtils logInWithPermissions:permissionsArray
                                block:^(PFUser *user, NSError *error) {
  if (user) {
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
      if (!error) {
        // Store the current user's Facebook ID on the user
        [[PFUser currentUser] setObject:[result objectForKey:@"id"]
                                 forKey:@"fbId"];
        [[PFUser currentUser] saveInBackground];
      }
    }];
  }
}];

然后,当您准备搜索用户的朋友时,您会发出另一个请求:

// Issue a Facebook Graph API request to get your user's friend list
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
  if (!error) {
    // result will contain an array with your user's friends in the "data" key
    NSArray *friendObjects = [result objectForKey:@"data"];
    NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
    // Create a list of friends' Facebook IDs
    for (NSDictionary *friendObject in friendObjects) {
        [friendIds addObject:[friendObject objectForKey:@"id"]];
    }

    // Construct a PFUser query that will find friends whose facebook ids
    // are contained in the current user's friend list.
    PFQuery *friendQuery = [PFUser query];
    [friendQuery whereKey:@"fbId" containedIn:friendIds];

    NSArray *friendUsers = [friendQuery findObjects];
  }
}];

致以最诚挚的问候,