iosiphonefacebookios8.2

How get User's Friends list that contain Username and ids using Facebook iOS SDK 4.0


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

Solution

  • 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.