0

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

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

在此处输入图像描述

这是我尝试过的:

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 以请求不在应用程序内部的权限,并且朋友数组允许 nill 或 count = 0。

4

1 回答 1

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

     }];
}

希望这段代码对你有用。

于 2014-05-08T08:46:34.770 回答