Rani
Rani

Reputation: 3453

How to fetch the list of Facebook groups in iPhone?

I am creating an application which has Facebook integrated in it. I have incorporated Facebook which does getting the friends list, etc but I have a problem in retrieving the group list of a particular user.

Upvotes: 0

Views: 301

Answers (2)

David Ames Holland
David Ames Holland

Reputation: 1

Here is a more complete example of how to get just your own groups:

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

Upvotes: 0

Bhupendra
Bhupendra

Reputation: 2545

you need to add "friends_groups" to your permissions array that you used before opening a session. then you can fetch the group names :)

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

        }
    }

}

Upvotes: 1

Related Questions