获取Facebook好友列表并在自定义视图中显示?

时间:2014-05-08 08:31:29

标签: ios objective-c ios7 facebook-sdk-3.0 facebook-friends

我正在尝试获取FaceBook Friend列表,并将其显示在自定义视图中,例如“foresqure App”

我的意思是“foursquare”要求获得许可并在应用程序内的墙上发布,而无需将应用程序留给浏览器或Facebook应用程序?之后应用程序获取所有朋友并将其显示在自定义tableView中。

enter image description here

这是我试过的:

if (!FBSession.activeSession.isOpen) {
    // if the session is closed, then we open it here, and establish a handler for state changes
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                       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];
                                          NSLog(@"aaaa");
                                          FBRequest* friendsRequest = [FBRequest requestForMyFriends];
                                          [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                                        NSDictionary* result,
                                                                                        NSError *error) {
                                              NSArray* friends = [result objectForKey:@"data"];
                                              NSLog(@"Found: %i friends", friends.count);
                                              for (NSDictionary<FBGraphUser>* friend in friends) {
                                                  NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
                                              }
                                          }];
                                      }
                                  }];

}

在这种情况下,它将打开safari以询问不在app内的permision,而friend数组允许nill或count = 0。

1 个答案:

答案 0 :(得分:0)

试试这段代码..

首先像这样调用这个方法..

[self FBFriendList];

并添加这两种方法。

-(void)FBFriendList
{
if (!FBSession.activeSession.isOpen)
{
    // if the session is closed, then we open it here, and establish a handler for state changes

    [FBSession openActiveSessionWithReadPermissions:nil 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];
             [alertView release];
         }
         else if(session.isOpen)
         {
             [self FBFriendList];
             [self fb_frnd_use_this_app];
         }
     }];

    return;
}
}

-(void)fb_frnd_use_this_app
   {
    FBRequest *request =  [FBRequest  requestWithGraphPath:@"me/friends"
                                                parameters:@{@"fields":@"name,installed,first_name,picture"}
                                                HTTPMethod:@"GET"];

    [request startWithCompletionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error)
     {


         NSLog(@"%@",result);
         int count = 0;
         NSMutableArray *frnd_arr = [result objectForKey:@"data"];
         for (int i = 0; i < [frnd_arr count]; i++)
         {
             NSLog(@"%@",frnd_arr);
             count++;
         }

         NSLog(@"%@",app.Arr_Facebook_Frnd);
         app.fbfriendCount =[NSString stringWithFormat:@"%d",count];

     }];
}

希望此代码对您有用。