2012-04-25 60 views
1

我剛剛使用API​​,所以我不確定如何正確執行此操作。使用Facebook iOS SDK檢索生日

我試圖讓所有的用戶朋友的生日。到目前爲止,我成功地獲得了他們的friendList,然後理論上我可以簡單地ping每個ID並從中獲得生日。

但是我不確定如何實現這些方法。

當我的viewController負載:[facebook requestWithGraphPath:@"me/friends" andDelegate:self];

這發出關閉請求,我實現FBRequestDelegate方法來接受它:

-(void)request:(FBRequest *)request didLoad:(id)result{ 
    NSLog(@"result is : %@", result); 
} 

完美的作品,到目前爲止,我得到一個對象與所有的朋友的名字和ID。但是,現在我想遍歷每個ID,併發送另外幾百個請求。我已經知道如何設置循環和請求調用,但是我已經在這個viewController中使用了didLoad方法,並且我顯然需要在數據對象返回後以不同方式處理數據。

與(FBRequest *)有什麼關係?那是什麼,也許我可以像if(request == something)那樣呢?謝謝

回答

8

您可以使用一個fql請求檢索您(或用戶)的Facebook朋友的生日。

你可以閱讀有關FQL這裏:http://developers.facebook.com/docs/reference/fql/

但這裏是供參考(此代碼是定義爲一個FBSessionDelegate一個類,FBRequestDelegate)

- (void)request:(FBRequest *)request didLoad:(id)result { 
    // result is a NSDictionary of your friends and their birthdays! 
} 

- (void)fbDidLogin { 
    //some best practice boiler plate code for storing important stuff from fb 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 

    //now load all my friend's birthdays 
    NSMutableDictionary * params = 
         [NSMutableDictionary dictionaryWithObjectsAndKeys: 
         @"select birthday, name, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name", 
         @"query", 
         nil]; 

    [self.facebook requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self]; 
} 

- (void) loginWithFacebook { 
    self.facebook = [[[Facebook alloc] initWithAppId:@"<your app id here>" andDelegate:self] autorelease]; 
    //IMPORTANT - you need to ask for permission for friend's birthdays 
    [facebook authorize:[NSArray arrayWithObject:@"friends_birthday"]]; 
} 
+1

請您考慮接受我的回答,因爲您接受我的回答與您的其他帖子有關嗎?謝謝 – 2012-04-27 01:10:40

+0

很好的答案.. @ obuseme – 2012-07-27 11:27:40

+0

這個答案適合你嗎? – 2012-11-29 23:43:06

0

您可以繼承NSObject,將其稱爲「BirthdayLoader」並在其中實現請求。現在,在您的控制器中,只需創建這些加載器對象的實例即可檢索生日。這些可以在成功檢索生日時使用委託方法進行報告。

@protocol BirthDayLoaderDelegate 
-(void)didFinishLoadingRequest:(FBRequest *)request withResult:(id)result; 
@end 
2

您應使用此代碼的一些代碼(我Hackbook代碼示例使用它,它完美的作品):

在APICallsViewController.m添加這些功能:

/* 
/* Graph API: Method to get the user's friends. 
*/ 
- (void)apiGraphFriendsWithBirthdays { 
     [self showActivityIndicator]; 
     HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
     @"picture,id,name,link,birthday,gender,last_name,first_name",@"fields", 
nil]; 

     [[delegate facebook] requestWithGraphPath:@"me/friends" andParams:params andHttpMethod:@"GET" andDelegate:self]; 

}

以上將獲取大量數據,你只能使用ID,姓名和生日...

/* 
* Helper method to first get the user's friends and birthdays then 
* do something with it. 
*/ 
- (void)getFriendsForSetBirthday { 
    // Call the friends Birthday API first 
    currentAPICall = kAPIFriendsForSetBirthday; 
    [self apiGraphFriendsWithBirthdays]; 
} 

它添加到「 - (空)要求:(FBRequest *)要求didLoad:案件節(ID)結果」:

case kAPIFriendsForSetBirthday: 
    { 
     NSMutableArray *friends = [[NSMutableArray alloc] initWithCapacity:1]; 
     NSArray *resultData = [result objectForKey:@"data"]; 
     if ([resultData count] > 0) { 
      for (NSUInteger i=0; i<[resultData count] && i < 25; i++) { 
       [friends addObject:[resultData objectAtIndex:i]]; 

       NSDictionary *friend = [resultData objectAtIndex:i]; 
       long long fbid = [[friend objectForKey:@"id"]longLongValue]; 
       NSString *name = [friend objectForKey:@"name"]; 
       NSString *birthday = [friend objectForKey:@"birthday"]; 
       NSLog(@"id: %lld - Name: %@ - Birthday: %@", fbid, name,birthday); 


      } 

     } else { 
      [self showMessage:@"You have no friends."]; 
     } 
     [friends release]; 
     break; 


    } 

你需要爲生日閱讀權限要求(我做這件事是喜歡允許部分,但只要你喜歡,你可以做到這一點,需要注意的是你必須在它之前請求它可以工作):

- (void)apiPromptExtendedPermissions { 
    currentAPICall = kDialogPermissionsExtended; 
    HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSArray *extendedPermissions = [[NSArray alloc] initWithObjects:@"user_likes",@"friends_birthday", nil]; 
    [[delegate facebook] authorize:extendedPermissions]; 
    [extendedPermissions release]; 
} 

在.h文件中不要忘記添加kAPIFriendsForSetBirthday爲apicall。

在Dataset.m補充:

NSDictionary *graphMenu7 = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"Get friends birthday", @"title", 
           @"You can get all friends birthdays.", @"description", 
           @"Get friends birthday", @"button", 
           @"getFriendsForSetBirthday", @"method", 
           nil]; 

,並添加graphMenu7到菜單上的文件,不要忘記將其釋放。

我測試過它,它的工作完美, 希望這會有所幫助。

+1

不錯的answer.it幫我.. :) – 2012-07-30 10:47:27

+1

很高興我可以幫助。 – Idan 2012-10-05 10:51:45

0

考慮到要獲得生日(以及其他一些屬性,如用戶生物),應用程序必須由Facebook進行審查。否則,雖然權限被正確地賦予,但Graph API調用將不會檢索此屬性。