如何在iOS中从Facebook获取用户组?

时间:2014-08-06 10:22:09

标签: ios xcode facebook

-(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);
                          }
                      }];
}

我使用FBGraph在我的应用中集成了facebook。现在我需要显示用户组,包括组成员。 **更新显示完整修复。

为此,我有以下代码:

[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;
                                  }

                              } ];

它始终返回FBSessionStateClosedLoginFailed。为什么会这样?

1 个答案:

答案 0 :(得分:1)

请务必记住FBSession openActiveSessionWithReadPermissions:用于已开放的Facebook会话。因此,如果您没有已经打开的Facebook会话,则此方法将无效。此外,当您的会话实际开放时,请务必使用user_groups而不是groups来询问用户的群组信息。