0

我正在创建一个集成了 Facebook 的应用程序。我已经合并了 Facebook,它确实获取了朋友列表等,但我在检索特定用户的群组列表时遇到了问题。

4

2 回答 2

1

您需要将“friends_groups”添加到您在打开会话之前使用的权限数组中。然后你可以获取组名:)

    FBRequest* req = [FBRequest requestForGraphPath:@"/me/friends?fields=id,name,groups"];
    FBRequestConnection* reqconn = [[FBRequestConnection alloc] init];

    [reqconn addRequest:req completionHandler:^(FBRequestConnection *connection,
                                                id result,
                                                NSError *error){
        FBGraphObject *graphObject = result;
        id<FBGraphUser> graphUser = (id <FBGraphUser>)graphObject;

        [self getGroupsFromArray:[graphUser objectForKey:@"data"]];


    }];

    [reqconn start];

- (void)getGroupsFromArray:(NSArray *)listOfFriends
{

    for (id element in listOfFriends)
    {                
        NSDictionary *dict = [element objectForKey:@"groups"];
        NSArray *groupsArray = [dict objectForKey:@"data"];

        for (id groups in groupsArray)
        {
            NSString *groupName = [groups objectForKey:@"name"];
            NSLog(@"group name for person %@ is %@", [element valueForKeyPath:@"name"],groupName);

        }
    }

}
于 2013-02-12T10:22:58.293 回答
0

这是一个更完整的示例,说明如何仅获取您自己的组:

// list the permissions you want
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_groups",
                        nil];

// handle Facebook Login preliminaries...
if (!FBSession.activeSession.isOpen) {
    // if the session is closed, then we open it here, and establish a handler for state changes
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       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;
}

// set the GRAPH API query
FBRequest* req = [FBRequest requestForGraphPath:@"/me/?fields=name,groups"];

// create pointer to a backend connection
FBRequestConnection* reqconn = [[FBRequestConnection alloc] init];

// attach request to connection
[reqconn addRequest:req completionHandler:^(FBRequestConnection *connection,
                                            id result,
                                            NSError *error){
    NSDictionary *resultDict = (NSDictionary<FBGraphUser> *) result;

    NSString *path;

    path = [resultDict objectForKey:@"group"];


    if (resultDict == nil)
    {
        // This means the dictionary does not contain anything
        NSLog (@"Don't know the path to the groups");
    }
    else
    {
        // Print out the data (including groups).

        for(NSString *key in [resultDict allKeys]) {
            NSLog(@"%@, %@",key, [resultDict objectForKey:key]);
        }
    }

}];

// actually invoke the request
[reqconn start];
于 2013-08-03T13:30:42.013 回答