2015-01-03 102 views
0

我是iOS開發新手,我正嘗試將我的應用程序與Facebook集成。 我已經通過登錄的步驟,現在我正在嘗試獲取用戶朋友列表。保存Facebook的好友 - iOS

我需要在';'分隔列表中的用戶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; 
} 

現在,當我調試電話,我可以看到,我得到了用戶的朋友,但問題是,當我得到回報statemtn,好像我的變量「 userFBFriends「爲空......

任何想法我在這裏失蹤?


下面,我得到了我要留言下面的代碼,我現在也許有人可以幫我這個問題的答案。

- (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調用在甚至生成好友列表之前正在運行。 如果您在上面的代碼中看到我用BOLD標記了它。

在此先感謝您的幫助!

回答

0

您需要注意的是,您的代碼不會在單行中運行。代碼中有幾個部分(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. 
} 
+0

塞巴斯蒂安感謝您的偉大的答案。它幫助我更好地理解它,但我仍然遇到問題,我只是用一些新的代碼和問題編輯了我的原始帖子。我會感謝任何幫助。 –

+0

不客氣。我更新了我的代碼段。 –

+0

謝謝塞巴斯蒂安!我想我現在都準備好了!非常感謝您的快速響應和幫助。 –