How to get the list of friend without opening FBFriendPickerViewController iOS

与世无争的帅哥 提交于 2019-12-09 03:34:28

问题


I am using Facebook sdk 3.2 for iOS,
There are various sample project available for iOS
But my problem is how can i get the list of friends without opening FBFriendPickerViewController.
In fact i want all my friends facebook id and there Any help will be appreciated.
Thanks in Advance


回答1:


Try this:

get friend list using

**[FBRequest requestForMyFriends];**

**FBRequest* friendsRequest = [FBRequest requestForMyFriends];**

[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
NSArray* friends = [result objectForKey:@"data"];   ......

the coding is as follow but main line is

 **[FBRequest requestForMyFriends];**

-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
switch (state) {
    case FBSessionStateOpen: {
        if (self != nil) {
            [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                if (error) {
                    //error
                }else{
                    FBRequest* friendsRequest = [FBRequest requestForMyFriends];
                    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
                        NSArray* friends = [result objectForKey:@"data"];

                        for (int i = 0; i < [arrFacebookFriends count]; i++) {
                            UserShare *shareObj = [arrFacebookFriends objectAtIndex:i];
                            [shareObj release];
                            shareObj = nil;
                        }
                        [arrFacebookFriends removeAllObjects];

                        for (NSDictionary<FBGraphUser>* friend in friends) {
                            UserShare *shareObj = [[UserShare alloc] init];
                            shareObj.userName = friend.name;
                            shareObj.userFullName = friend.username;
                            shareObj.userId = [friend.id intValue];
                            NSLog(@"%@",friend.id);
                            shareObj.userPhotoUrl = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?", friend.id];
                            [arrFacebookFriends addObject:shareObj];
                            [shareObj release];
                        }
                        [self StopSpinner];
                        [tblFacebookFriends reloadData];
                    }];
                }
            }];
        }
        FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
        [cacheDescriptor prefetchAndCacheForSession:session];
    }
        break;
    case FBSessionStateClosed: {
        [self StopSpinner];
        UIViewController *topViewController = [self.navigationController topViewController];
        UIViewController *modalViewController = [topViewController modalViewController];
        if (modalViewController != nil) {
            [topViewController dismissViewControllerAnimated:YES completion:nil];
        }
        //[self.navigationController popToRootViewControllerAnimated:NO];

        [FBSession.activeSession closeAndClearTokenInformation];

        [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
    }
        break;
    case FBSessionStateClosedLoginFailed: {
        [self StopSpinner];
        [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
    }
        break;
    default:
        break;
}

[[NSNotificationCenter defaultCenter] postNotificationName:SCSessionStateChangedNotificationFL object:session];

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@", [FacebookFriendsListViewController FBErrorCodeDescription:error.code]] message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}
}


来源:https://stackoverflow.com/questions/15709226/how-to-get-the-list-of-friend-without-opening-fbfriendpickerviewcontroller-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!