Question

I am building an iOS application that is connected to Facebook. I got most of the Facebook related code (pretty much copied all of it) from the TokenCacheHowTo project. I have run into two problems, one which had a simple fix, but I think may be causing my second problem.

At first the login wasn't persisting. I had copied all the code from TokenCacheHowTo, so I was confused why and looked for a solution. Eventually I found this question and it said that if I removed @[@"basic_info"] from the permissions parameter it would work. And it did. I am still confused as to why it would work. Note: I removed @[@"basic_info"] from the openSessionWithAllowLoginUI method in my app delegate.

My problem now is that I want a list of the users friends, so I do the following request in the app delegate.

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    NSLog(@"Under");
    NSArray* friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends", friends.count);
}];

This code outputs Found: 0 friends within my app delegate, but if I put the code in the same place in TokenCacheHowTo it outputs Found: X friends where X is my actual number of friends. This is confusing to me because I am able to get the users information such as their id and name from Facebook and store it on my server.

I realize that there is probably a simple solution to this but I don't have a good grasp of the Facebook SDK yet and I'm stuck.

Was it helpful?

Solution

Try this as working fine for me and Ensure about the Permissions like

NSArray *array = [NSArray arrayWithObjects:@"user_friends", nil];

and then fetch all the friend

-(void)getFriendList   {
    FBRequest* friendsRequest = [FBRequest requestWithGraphPath:@"me?fields=friends.fields(first_name,last_name)" parameters:nil HTTPMethod:@"GET"];
    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                  NSDictionary* result,
                                                  NSError *error) {
        NSDictionary* friendsDic = [result objectForKey:@"friends"];
        NSArray *friends = [friendsDic objectForKey:@"data"];
        for (NSDictionary *friend in friends) {
            NSLog(@"firstname %@",friend[@"first_name"]);
            NSLog(@"last name %@",friend[@"last_name"]);
        }
     }];
}

OTHER TIPS

Nothing wrong with your code. I've used the same code which you used above and I got x friends list. But my permission is declared as below

FBSession.activeSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"user_about_me", @"email",@"user_birthday", nil]];

Note: I want email id and birthday from users that why my permissions include that fields. And It should be declared before authentication, then only sessions kept this permission as it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top