2013-02-12 81 views

回答

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

     } 
    } 

} 
+0

喜@Bhupendra我已經設置了權限,但我的問題是如何給facebook打電話給group api – Rani 2013-02-12 10:36:15

+0

我已經編輯了答案,請用這種方法檢查一下,你可以得到小組信息:) – Bhupendra 2013-02-12 11:20:15

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]; 
相關問題