2016-03-23 15 views
0

Uygulamamda ad, kimlik vb. Ile Facebook arkadaş listesini almaya çalışıyorum.iOS'ta Facebook Friend List'i Alın

- (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 ile giriş yapmayı ve profilimi almayı başarabiliyorum. Bundan sonra arkadaşların grafik api üzerinden arkadaş listesi almaya çalıştım. Ama o zaman, sadece aşağıdaki gibi takip eden arkadaşlar saydı.

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..."; 
     }; 
    }; 

Kimlik, ad vb. Gibi arkadaş listesiyle ilgili tüm bilgileri nasıl alabilirim? Daha önce uygulanan herhangi biri varsa lütfen bana yardımcı olun. Teşekkür ederim.

cevap

1

Bu çalışma benim için.

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); 
    }]; 
} 
0

result.paging.next erişimle belirteci ve tüm komple URL içeriyor.

NSString *nextPage = result[@"paging"][@"next"]; 
if (!nextPage) return; 
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:nextPage] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    id json = [self decodeJSONResult:data response:response error:error]; 
    // use json 
}] resume]; 

Bir FBSDKGraphRequest uzantısı olarak bu yazmış: FBSDKGraphRequest+Paging.h

Ben basit şey bir NSURLSession veri göreve üzerinde geçmek için sadece olduğunu bulmak
İlgili konular