Facebook iOS SDK - get friends list

后端 未结 11 1447
猫巷女王i
猫巷女王i 2020-12-07 12:41

Using the Facebook iOS SDK, how can I get an NSArray of all my friends and send them an invitation to my app? I am specifically looking for the graph path to ge

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 13:12

    Here is a more complete solution:

    In your header file:

    @interface myDelegate : NSObject  {
        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
    

    Then in your implementation:

    @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;
    }
    

    Then you need at least the following delegate protocol method:

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

提交回复
热议问题