保存 Facebook 好友 - iOS

Save Facebook Friends - iOS

我是 iOS 开发的新手,我正在尝试将我的应用程序与 Facebook 集成。 我已经通过了登录步骤,现在我正在尝试获取用户好友列表。

我需要在用“;”分隔的列表中包含用户 ID所以我有以下代码:

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

现在,当我调试调用时,我可以看到我确实得到了用户的朋友,但问题是当我到达 return statemtn 时,我的变量 "userFBFriends" 似乎是空的。 ...

知道我在这里遗漏了什么吗?


根据我得到的答案,我想 post 我现在拥有的以下代码,也许有人可以帮助我解决这个问题。

- (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);
}

现在我的问题是如何在实际调用 getUserFacebookFriendsList 的主函数中打印好友列表? 因为我在函数 "loginViewFetchedUserInfo" 中执行的 NSLOG 调用似乎在朋友列表生成之前就已经运行了。 如果您在我上面的代码中看到我将其标记为粗体。

在此先感谢您的帮助!

您需要注意,您的代码不会 运行 在一行中。您的代码 (FBRequestConnection) 中的某些部分将 运行 与您的其他代码并行。这叫做concurrency。您必须等到 FBRequestConnection 完成。

这是您更新后的代码:

- (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.
}