RamChandraReddy
RamChandraReddy

Reputation: 239

How do you fetch user groups from Facebook in iOS?

-(void)getFBGroupsWithsuccess:(SMSuccessResponseBlock) success
                  failure:(SMFailureBlock) failure{

__block NSMutableArray *groupsDataArray=[[NSMutableArray alloc]init];

[FBRequestConnection startWithGraphPath:@"me/groups?fields=picture,name"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error) {
                              // Sucess! Include your code to handle the results here
                              NSLog(@"user events: %@", result);

                              NSArray *resultsData=[NSArray arrayWithArray:(NSArray *)[result valueForKey:@"data"]];

                              if ([resultsData count]>0) {

                                  [resultsData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                                      NSString *groupId=[obj valueForKey:@"id"];
                                      NSString *groupName=[obj valueForKey:@"name"];
                                      NSString *groupImageURL=[[[obj valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
                                      [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/members",groupId]
                                                            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                                if (!error) {

                                                                    NSMutableDictionary *groupData=[[NSMutableDictionary alloc]init];
                                                                    [groupData setValue:groupId forKey:@"groupdID"];
                                                                    [groupData setValue:groupImageURL forKey:@"groupURL"];
                                                                    [groupData setValue:[result valueForKey:@"data"] forKey:@"groupMembers"];
                                                                    [groupData setValue:groupName forKey:@"groupdName"];


                                                                    [groupsDataArray addObject:groupData];
                                                                    // Sucess! Include your code to handle the results here

                                                                    if (idx==[resultsData count]-1) {
                                                                        success(groupsDataArray);
                                                                    }
                                                                    NSLog(@"user events: %@", result);
                                                                } else {
                                                                    // An error occurred, we need to handle the error
                                                                    // See: https://developers.facebook.com/docs/ios/errors
                                                                    NSLog(@"error: %@", error);

                                                                }
                                                            }];

                                  }];

                              }
                              else
                              {
                                  success(groupsDataArray);
                              }


                          } else {

                              failure(error);
                          }
                      }];
}

I integrated facebook in my app using FBGraph. Now I need to display user's groups including group members. **Update shows complete fix.

To accomplish this I have the following code:

[FBSession openActiveSessionWithReadPermissions:@[@"groups"]
                                   allowLoginUI:NO
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

                                  switch (state) {
                                      case FBSessionStateOpen:
                                          [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                                              if (error) {

                                                  NSLog(@"error:%@",error);


                                              }
                                              else
                                              {

                                              }
                                          }];
                                          break;

                                      case FBSessionStateClosed:
                                      case FBSessionStateClosedLoginFailed:
                                          [FBSession.activeSession closeAndClearTokenInformation];
                                          break;

                                      default:
                                          break;
                                  }

                              } ];

It always returns FBSessionStateClosedLoginFailed. Why is this happening?

Upvotes: 1

Views: 478

Answers (1)

davetw12
davetw12

Reputation: 1841

It's important to keep in mind that FBSession openActiveSessionWithReadPermissions: is used for Facebook sessions that are already open. So if you don't have a facebook session that is already open, this method will not work. Also, when your session is actually open, be sure to ask for the user's group information with user_groups not groups.

Upvotes: 1

Related Questions