1

I'm trying to Fetch User's friends List. my last working Code that i implement in FacebookSDK 3.12 is now of No use because facebook have changed all their classes and even deleted old Classes, below is my old Code:

#pragma mark - Facebook Methods -
-(void)initiateFacebookSessionIfNot
{
    if (FBSession.activeSession.isOpen)
    {
        [self getUserFriendsIds];
    } else
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_birthday",@"friends_hometown",
                                @"friends_birthday",@"friends_location",
                                nil];
        [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status,NSError *error)
         {
             if (error) {
                 NSString *alertMessage, *alertTitle;

                 if (error.fberrorShouldNotifyUser) {
                     alertTitle = @"Something Went Wrong";
                     alertMessage = error.fberrorUserMessage;
                 } else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
                     NSLog(@"user cancelled login");
                 } else {
                     // For simplicity, this sample treats other errors blindly.
                     alertTitle  = @"Unknown Error";
                     alertMessage = @"Error. Please try again later.";
                     NSLog(@"Unexpected error:%@", error);
                 }

                 if (alertMessage)
                 {
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
                                                                     message:error.fberrorUserMessage
                                                                    delegate:nil
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil];
                     [alert show];
                 }
             } else if (FB_ISSESSIONOPENWITHSTATE(status)) {
                 [self getUserFriendsIds];
             }
         }];
    }
}
-(void)getUserFriendsIds
{
    FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,username"];
    [ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSMutableArray *fbFriendIDs;
        NSMutableArray *fbFriendsName;
        NSMutableArray *fbFriendsUserName;

        NSMutableArray *data = [[NSMutableArray alloc]initWithArray:[result objectForKey:@"data"]];
        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if (!(NSDictionary*)obj[@"username"]) {

                NSDictionary *newDictionary =@{@"id": (NSDictionary*)obj[@"id"],
                                               @"name": (NSDictionary*)obj[@"name"],
                                               @"username": @"0"
                                               };
                [data replaceObjectAtIndex:idx withObject:newDictionary];
            }
        }];

        fbFriendsUserName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.username"]];
        fbFriendsName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.name"]];
        fbFriendIDs = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.id"]];

        NSDictionary *queryDictionary =
        @{@"user_id": userObj.user_id,
          @"friend_ids":[fbFriendIDs componentsJoinedByString:@","],
          @"names":[fbFriendsName componentsJoinedByString:@","],
          @"usernames":[fbFriendsUserName componentsJoinedByString:@","]
          };

        [[RequestHandler sharedClient] CheckFBFriendsWithDictionary:queryDictionary WithCompletionBlock:^(NSArray *TUNFriendsArray, NSArray *FBFriendsArray, NSString *errorMessage) {
            if (!errorMessage) {

                tunFriendsArray = [[NSMutableArray alloc]initWithArray:TUNFriendsArray];
//                fbFriendsArray = [[NSMutableArray alloc]initWithArray:FBFriendsArray];

                [_facebookTableView reloadData];
            }
            else{
                [ApplicationUtilities ShowAlertViewWithTitle:APP_NAME Message:errorMessage];
            }
        }];
    }];
    [SVProgressHUD dismiss];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

It's no longer possible to receive the username field, as well as every information of the user's friends other than the public_profile, because all the friends_* permissions have been removed since v2.0.

Furthermore, you don't receive the full friends list, but only those which also use your app.

See

Quotes:

  • /me/username is no longer available.

  • The /me/friends endpoint no longer includes the full list of a person's friends. Instead, it now returns the list of that person's friends who are also using your app.

  • All friends_* permissions have been removed.

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • 1
    snap, i was just about to send my answer. will upvote yours instead :) – andyrandy Mar 30 '15 at 09:34
  • as we know user id is the object shows on public_profile, so is this also not possible to get just user's ids – Waqas Haider Sheikh Mar 30 '15 at 09:44
  • @WaqasHaiderSheikh Not sure what you mean though... Baseline is the behavior you want to achieve is no longer possible. – Tobi Mar 30 '15 at 09:51
  • on currrent SDk im using /me/friends and it's giving me only one user information, please need your help clear me on this – Waqas Haider Sheikh Mar 30 '15 at 09:55
  • 1
    It's already in my answer. Second quote. I'd recommend that you read the docs I linked. It's all in there! That's the minimum effort one can expect I guess. – Tobi Mar 30 '15 at 09:57
  • 1
    yeah, it´s sad that people are too lazy to search on google or stackoverflow, or just take a look at the official docs and the changelog... – andyrandy Mar 30 '15 at 10:08
  • u guys are not helpful, the one who under estimate is never be a helpful guy! – Waqas Haider Sheikh Mar 30 '15 at 10:37
  • @WaqasHaiderSheikh What part of "it's no longer possible" is not clear to you? In fact we are helpful, we pointed you to the right place in the FB docs (which you could for sure have found yourself). If you don't read them in the first place, and even after I pointed them out, and also don't use the StackOverflow search (there are literally hundreds of questions asking the same stuff as you), then it's not our fault. – Tobi Mar 30 '15 at 10:52
  • @Tobi i say that because you guys react like weird, why people post question ? because dnt know or they need some help. you guys are using non professional word lazy and etc to demoralize. i'll Appreciate when you help me till end with nice behavior but unfortunately u guys help me but doesn't deserve any appreciation sad. – Waqas Haider Sheikh Mar 30 '15 at 10:57
  • 2
    _“why people post question ?”_ – well, why do people post this _particular_ question? Obviously because they could not be bothered to read up on stuff on their own beforehand … all that you have been told here is explicitly mentioned in the FB API changelogs, in the documentation, and has been discussed literally a thousand times before already. – CBroe Mar 30 '15 at 10:59
1

Facebook has changed a lot of implementation in their new version i.e. V2.0 in which Tag-gable Friends API, Invitable Friend API,Social Context API,Business Mapping API and Tagged Places API are the new features. They have also made many permission changes so as to increase security read more on this from the following link

Hope you will find your required implementation changes from the above links.

wassay khan
  • 133
  • 1
  • 8