在iOS中获取Facebook朋友列表 [英] Get Facebook Friend List in iOS

查看:147
本文介绍了在iOS中获取Facebook朋友列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用中获取具有名称,ID等的Facebook朋友列表.

I am trying to get Facebook friend list with name, id, etc in my app.

- (IBAction)onFBLogin:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
         } else {
             NSLog(@"%@", result);
             [self getFBEmailAddress];
        }
     }];
}
-(void)getFBEmailAddress{
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
                                       parameters:@{@"fields": @"picture, email, friends"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSString *pictureURL = [NSString stringWithFormat:@"%@",[result objectForKey:@"picture"]];
             mFBId = [NSString stringWithFormat:@"%@",[result objectForKey:@"id"]];
             NSLog(@"My Profile : %@", result);
             NSLog(@"email is %@", [result objectForKey:@"email"]);

             [self getFBFriendList];
         }
         else{
             NSLog(@"%@", [error localizedDescription]);
         }
     }];
}
-(void)getFBFriendList{

    NSString* graphPat = [[NSString alloc] initWithFormat:@"%@/friends", mFBId];

    FBSDKGraphRequest *requestFriends = [[FBSDKGraphRequest alloc]
                                         initWithGraphPath:graphPat
                                         parameters:@{@"fields": @"id, name"}
                                         HTTPMethod:@"GET"];
    [requestFriends startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                 id result,
                                                 NSError *error) {
        if (!error && result)
        {
            NSArray *allFriendsResultData = [result objectForKey:@"data"];

            if ([allFriendsResultData count] > 0)
            {
                for (NSDictionary *friendObject in allFriendsResultData)
                {
                    NSString *friendName = [friendObject objectForKey:@"name"];
                    NSString *friendID = [friendObject objectForKey:@"id"];
                    NSLog(@"%@ : %@", friendID, friendName);

                }
            }
        }

    }];}

我已经成功通过Facebook登录并获取了我的个人资料.之后,我尝试通过friends图表api获取朋友列表.但是当时,它只说了以下的朋友数.

I've succeed to login via Facebook and get my profile. After that, I've tried to get friend list via the friends graph api. But at that time, it only said count of friends following as below.

    friends =     {
        data =         (
        );
        summary =         {
            "total_count" = 2;
        };
    };
    id = 60XXXXXX1295X;
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/hprofile-xfa1/v/t1.0-1/p50x50/1..._501957533...";
        };
    };

如何获取朋友列表的完整信息,例如ID,姓名等?如果任何人已经实施,请帮助我.谢谢.

How Can I get full information of friends list such as id, name, etc? Please help me if any one already implemented. Thank you.

推荐答案

这对我有用.

NSMutableArray *completeList = [[NSMutableArray alloc] init];
[self fetchFacebookEachFriend:completeList withGrapPath:@"/me/friends" withCallback:^(BOOL success, NSMutableArray * fbList) {
     //Finish get All Friends
     fbListFinal(success, fbList);
}];


- (void)fetchFacebookEachFriend:(NSMutableArray *)completeList withGrapPath:(NSString *)graphPath withCallback:(returnFbList)fbList
{
    NSDictionary *limitDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"first_name, last_name, middle_name, name, email", @"fields", nil];
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:limitDictionary HTTPMethod:@"GET"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (error) {
            [[[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Please connect to Facebook to find your friends" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
            fbList(NO, completeList);
            return;
        }

        //Add each friend to list
        for (NSDictionary *friend in [result valueForKey:@"data"]) {
            [completeList addObject:friend];
        }

        if ([result valueForKey:@"paging"] != nil && [[result valueForKey:@"paging"] valueForKey:@"next"] != nil) {
            NSString *nextPage = [[result valueForKey:@"paging"] valueForKey:@"next"];
            nextPage = [[nextPage componentsSeparatedByString:@"?"] objectAtIndex:1];
            [self fetchFacebookEachFriend:completeList withGrapPath:[NSString stringWithFormat:@"me/friends?%@",nextPage] withCallback:fbList];
        } else
            fbList(YES, completeList);
    }];
}

这篇关于在iOS中获取Facebook朋友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆