Facebook iOS SDK - 获取好友列表

use*_*064 32 cocoa-touch facebook objective-c ios facebook-friends

使用Facebook iOS SDK,我如何获得NSArray所有朋友的邀请并向我们的应用发送邀请?我特意寻找获取所有朋友的图形路径.

Jef*_*eff 74

使用Facebook SDK 3.0,您可以执行以下操作:

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                  NSDictionary* result,
                                  NSError *error) {
    NSArray* friends = [result objectForKey:@"data"];
    NSLog(@"Found: %lu friends", (unsigned long)friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.objectID);
    }
}];
Run Code Online (Sandbox Code Playgroud)

  • 它只返回将使用该应用程序的朋友.但是如何获取我FB中所有朋友的列表? (4认同)
  • 在最新的facebook朋友sdk中,你无法直接获取朋友列表. (4认同)
  • 你有没有要求过friends_birthday权限? (2认同)
  • 终于得到了解决方案http://stackoverflow.com/questions/13170850/ios-facebook-sdk-3-1-retrieve-friend-birthday-returning-null (2认同)

Jef*_*688 14

这是一个更完整的解决方案:

在您的头文件中:

@interface myDelegate : NSObject <UIApplicationDelegate, FBSessionDelegate, FBRequestDelegate> {
    Facebook *facebook;
    UIWindow *window;
    UINavigationController *navigationController;

    NSArray *items; // to get facebook friends
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) NSArray *items;
@end
Run Code Online (Sandbox Code Playgroud)

然后在你的实现中:

@implementation myDelegate

@synthesize window;
@synthesize navigationController;
@synthesize facebook;
@synthesize items;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

...


    facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID_FROM_FACEBOOK" andDelegate:self];

    [facebook requestWithGraphPath:@"me/friends" andDelegate:self];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

那么您至少需要以下委托协议方法:

- (void)request:(FBRequest *)request didLoad:(id)result {
    //ok so it's a dictionary with one element (key="data"), which is an array of dictionaries, each with "name" and "id" keys
    items = [[(NSDictionary *)result objectForKey:@"data"]retain];
    for (int i=0; i<[items count]; i++) {
        NSDictionary *friend = [items objectAtIndex:i];
        long long fbid = [[friend objectForKey:@"id"]longLongValue];
        NSString *name = [friend objectForKey:@"name"];
        NSLog(@"id: %lld - Name: %@", fbid, name);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 10

获取您可以使用的朋友列表

https://graph.facebook.com/me/friends

[facebook requestWithGraphPath:@"me/friends"
                     andParams:nil
                   andDelegate:self];
Run Code Online (Sandbox Code Playgroud)

要了解有关所有可能API的更多信息,请阅读

https://developers.facebook.com/docs/reference/api/


Joh*_*oza 8

也许这可能有所帮助

[FBRequestConnection startForMyFriendsWithCompletionHandler:
 ^(FBRequestConnection *connection, id<FBGraphUser> friends, NSError *error) 
  { 
     if(!error){
       NSLog(@"results = %@", friends);
     }
  }
];
Run Code Online (Sandbox Code Playgroud)