简体   繁体   English

如何在iOS的FACEBOOK SDK中从Facebook获取朋友列表?

[英]How to fetch friends list from facebook in FACEBOOK SDK in iOS?

I have a button, I want to connect to facebook as I click it, I want all the friends in my facebook. 我有一个按钮,我想在单击它时连接到Facebook,我希望我的Facebook中的所有朋友。

I can access facebook and I get Facebook Token and saving it in Database. 我可以访问facebook,并获取Facebook令牌并将其保存在数据库中。

I am connecting to facebook using following code in CONTROLLER A, but in CONTROLLER B, I want to fetch friend list. 我正在使用控制器A中的以下代码连接至Facebook,但是在控制器B中,我想获取朋友列表。

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                        user:(id<FBGraphUser>)user {


NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;

NSLog(@"Token is %@", fbAccessToken);

DataManager *dataManager = [[DataManager alloc] init];

/*flag is for identification, that from which account user has logged in, either facebook or

 from account which he made using this app.

 flag = facebook //if user is signed in using his facebook account
 flag = myuser //if user is signed in using his own app account

 */
[dataManager saveloginData:fbAccessToken username:@"NO DATA" password:@"NO DATA" flag:@"facebook"];

 //   NSLog(@"Veer Suthar %@",user);

status = YES;

[self loginWithFacebookDirectly];

// here we use helper properties of FBGraphUser to dot-through to first_name and
// id properties of the json response from the server; alternatively we could use
// NSDictionary methods such as objectForKey to get values from the my json object
self.labelFirstName.text = [NSString stringWithFormat:@" %@", user.first_name];

// setting the profileID property of the FBProfilePictureView instance
// causes the control to fetch and display the profile picture for the user
self.profilePic.profileID = user.id;

NSLog(@"USER IS %@", user);


// self.loggedInUser = user;
}

Try like this.... 像这样尝试...

// To fetch friends list //获取好友列表

     -(void)addList:(FBSession *)session 
     {
       NSString* fql = [NSString stringWithFormat: @\"select uid from user where uid == %lld\", session.uid];
       NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@\"query\"];
       sessionView = session;
       [[FBRequest requestWithDelegate:self] call:@\"facebook.friends.get\" params:params];      
     }

     - (void)request:(FBRequest*)request didLoad:(id)result 
       {
       if(myList==nil) 
        {
        NSArray* users = result;
        myList =[[NSArray alloc] initWithArray: users];
        for(NSInteger i=0;i<[users count];i++) {
        NSDictionary* user = [users objectAtIndex:i];
        NSString* uid = [user objectForKey:@\"uid\"];
        NSString* fql = [NSString stringWithFormat: @\"select name from user where uid == %@\", uid];
        NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@\"query\"];
        [[FBRequest requestWithDelegate:self] call:@\"facebook.fql.query\" params:params];
       }
         }
       else 
       {
      NSArray* users = result;
      NSDictionary* user = [users objectAtIndex:0];
      NSString* name = [user objectForKey:@\"name\"];
      txtView.text=[NSString localizedStringWithFormat:@\"%@%@,\n\",txtView.text,name];
       }
       }

//To list the online friends //列出在线好友

   - (void)session:(FBSession*)session didLogin:(FBUID)uid {
      NSString *fql = [NSString localizedStringWithFormat:@\"SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=%lld) AND 'active' IN online_presence\",uid];
      myList=nil;
      NSDictionary *params =[NSDictionary dictionaryWithObject:fql forKey:@\"query\"];
      [[FBRequest requestWithDelegate:self] call:@\"facebook.fql.query\" params:params];
     }
  - (void)request:(FBRequest*)request didLoad:(id)result {
     if(myList==nil) {
       NSArray* users = result;
       myList =users;
       for(int i=0;i<[users count];i++) {
       NSDictionary* user = [users objectAtIndex:i];
       NSString* name = [user objectForKey:@\"uid\"];
       NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:name,@\"uids\",@\"name\",@\"fields\",nil];
       [[FBRequest requestWithDelegate:self] call:@\"facebook.users.getInfo\" params:params];
      }
  }
  else {
       NSArray* users = result;
       NSDictionary* user = [users objectAtIndex:0];
       NSString* name = [user objectForKey:@\"name\"];
      NSLog(name);
   }
  }

Hope it will helps you.. 希望对您有帮助。

Try this: 尝试这个:

- (void)userFriendList {

    NSString *query =@"SELECT name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend where uid1 = me())";

    // Set up the query parameter
    NSDictionary *queryParam =
    [NSDictionary dictionaryWithObjectsAndKeys:query, @"q", nil];
    // Make the API request that uses FQL
    [FBRequestConnection startWithGraphPath:@"/fql"
                                 parameters:queryParam
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
                              if (error) {
                                  NSLog(@"Error: %@", [error localizedDescription]);
                              } else {
                                  NSLog(@"Result: %@", result);
                                  // show result
                                  self.friendList = (NSArray *) [result objectForKey:@"data"];

                                  }
                          }];
}

Here in the above self.friendList is a NSMutableArray 在上面的self.friendList是一个NSMutableArray

This is how I fetch Friend List in an Array and then placed in UITableView 这是我在数组中获取朋友列表,然后放置在UITableView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM