简体   繁体   English

保存Facebook好友-iOS

[英]Save Facebook Friends - iOS

I'm new to iOS development and I'm trying to integrate my app with Facebook. 我是iOS开发的新手,正在尝试将我的应用程序与Facebook集成。 I'm already passed the step of the login and now I'm trying to get the user friends list. 我已经通过了登录步骤,现在我正在尝试获取用户朋友列表。

I need to have the users IDs in a list seperated by ';' 我需要用“;”分隔列表中的用户ID。 so I have the following code in place: 所以我有以下代码:

-(NSMutableString *) getUserFacebookFriendsList
{    
    NSMutableString *userFBFriends = [[NSMutableString alloc] init];

    [FBRequestConnection startWithGraphPath:@"/me/friends" 
                                 parameters:nil 
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        for (NSDictionary *friendData in [result objectForKey:@"data"])
        {
            NSMutableString  *friendId = [friendData objectForKey:@"id"];
            [userFBFriends appendString:friendId];
            [userFBFriends appendString:@";"];
            NSString *FBFriendsList = [NSString stringWithFormat:userFBFriends];
        }
    }];

    return userFBFriends;
}

Now when I debug the call I can see that I do get the user's friends but the problem is when I get to the return statemtn, it seems like my variable "userFBFriends" is empty.... 现在,当我调试呼叫时,我可以看到我确实获得了用户的朋友,但是问题是当我到达返回状态mtn时,似乎我的变量“ userFBFriends”为空。

Any idea what I am missing here ? 知道我在这里缺少什么吗?


Following the answer that I got I would like to post the following code that I have now and maybe someone can help me with the problem. 按照我得到的答案,我想发布下面的代码,也许有人可以帮助我解决这个问题。

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    NSLog(@"test login facebook");
    NSLog(@"the facbook user borthday is : %@",[user birthday]);
    NSLog(@"The FACEBOOK user ID is : %@", [user objectID]);
    NSLog( @"### running FB sdk version: %@", [FBSettings sdkVersion] );
   // FBRequestConnection* conn = [[FBRequestConnection alloc] init];


    NSLog(@"User's friends list as will be sent to the server is : %@", **XXXXXXX** );

}


-(void) getUserFacebookFriendsList {
    NSMutableString *userFBFriends = [[NSMutableString alloc] init];


    [FBRequestConnection startWithGraphPath:@"/me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        for (NSDictionary *friendData in [result objectForKey:@"data"])
        {
            NSMutableString  *friendId = [friendData objectForKey:@"id"];
            [userFBFriends appendString:friendId];
            [userFBFriends appendString:@";"];

            // This will call -didLoadFacebookFriends on the main thread.
            __weak __typeof(self) weakSelf = self;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf didLoadFacebookFriends:userFBFriends];
                });
        }
    }];
}

-(void) didLoadFacebookFriends:(NSMutableString *)facebookFriends {
    // Do your stuff here.
    NSLog(@"The list of friends is : %@",facebookFriends);
}

Now my question is how can I get the friends list to be printed in the main function that actually do the call to the getUserFacebookFriendsList ? 现在我的问题是,如何才能在实际上调用getUserFacebookFriendsList的主函数中打印要打印的朋友列表? Because again it seems like the NSLOG call that I'm doing in the function "loginViewFetchedUserInfo" is running before the friends list was even generated. 因为同样,在好友列表生成之前,我在函数“ loginViewFetchedUserInfo”中执行的NSLOG调用似乎正在运行。 If you see in my code above I marked it in BOLD. 如果您在上面的代码中看到,则将其标记为粗体。

Thanks in advance for helping ! 在此先感谢您的帮助!

You need to be aware that your code does not run in a single line. 您需要注意,您的代码不能单行运行。 There are sections in your code ( FBRequestConnection ) that will run in parallel to your other code. 您的代码( FBRequestConnection )中的某些部分将与其他代码并行运行。 This is called concurrency . 这称为并发 You'll have to wait till FBRequestConnection finished. 您必须等到FBRequestConnection完成。

Here's your updated code: 这是您的更新代码:

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    NSLog(@"test login facebook");
    NSLog(@"the facbook user borthday is : %@",[user birthday]);
    NSLog(@"The FACEBOOK user ID is : %@", [user objectID]);
    NSLog( @"### running FB sdk version: %@", [FBSettings sdkVersion] );

    [self getUserFacebookFriendsList];         
}


-(void) getUserFacebookFriendsList {
    NSLog("%@", @"Requesting facebook friends");

    [FBRequestConnection startWithGraphPath:@"/me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSLog("%@", @"Request finished.");

        NSMutableString *userFBFriends = [[NSMutableString alloc] init];

        for (NSDictionary *friendData in [result objectForKey:@"data"]) {
            NSMutableString  *friendId = [friendData objectForKey:@"id"];
            [userFBFriends appendString:friendId];
            [userFBFriends appendString:@";"];
         }

         NSLog(@"%@", @"Finished parsing. Will call didLoadFacebookFriends:");
         __weak __typeof(self) weakSelf = self;
         dispatch_async(dispatch_get_main_queue(), ^{
             [weakSelf didLoadFacebookFriends:userFBFriends];
         });            
    }];
}

-(void) didLoadFacebookFriends:(NSString *)facebookFriends {
    NSLog(@"The list of friends is : %@",facebookFriends);

    // Do what you need to do.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM