2012-09-28 57 views
3

我在我的.h文件中實現FBFriendPickerDelegate並使用下面的委託來獲取信息:如何在iOS中使用Facebook SDK檢索朋友的生日?

-(BOOL)friendPickerController:(FBFriendPickerViewController *)friendPicker 
shouldIncludeUser:(id <FBGraphUser>)user 
{ 
NSLog(@"Birthday:%@",user.birthday); 
return YES; 
} 

但NSLog的每一次我得到一個空值...關注的是結果。我在NSLog中獲得:

Birthday:(null) 2012-09-28 10:20:22.450 
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.451 
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.452 
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.454 
MyFacebookApp[455:207] Birthday:(null) 

請告訴我是否做錯了什麼。

回答

0

我認爲我們不能得到朋友的用戶名,生日和地點,但我們可以得到登錄用戶名,生日和所有data.if你找到任何解決方案,然後請讓我知道。

+0

如果我們使用NSLog(@「Data:%@」,user);在這種方法中,我們可以得到像「first_name,id,last_name,name,picture」這樣的數據,但它仍然不清楚如何獲得生日和位置。 – RoHaN

+0

亞那些即將到來,但生日,用戶名,位置不來。 – iSpark

3

您是否首先詢問正確的permissions

您需要user_birthdayfriends_birthday來訪問用戶的resp。他們的朋友的生日,並user_location/friends_location獲取他們的當前位置。

儘管當前用戶可能會爲您提供這些朋友的權限,但這並不一定意味着您將獲得所有朋友的請求數據 - 是否允許應用程序代表朋友訪問該信息取決於這些用戶的個人設置。

特別是對於生日場,它可能是,你會得到只有日和月,但不是一年。或者,當然,就像其他任何信息一樣,什麼也不是。

+0

是的,我已經使用這些權限.....所以有什麼替代方法來獲得朋友的生日或地點的..... – RoHaN

+0

非常感謝你非常非常。 –

0

在將朋友列表添加到朋友列表之前,您需要查詢好友列表中的每個用戶。

默認情況下,朋友列表中的FBGraphUser對象只包含2個字段:ID和名稱。 更多的信息可以通過查詢每個朋友的用戶ID來獲得。

使用圖形API的一個例子:

[FBRequestConnection startWithGraphPath:[friend id] 
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
// result is your FBGraphUser object for the queried friend 
} 

更新:我忽略了這個:有一個辦法讓所有的朋友和使用查詢參數字段他們的生日。在這裏你可以指定你需要的字段。 (如果你有許可)

http://graph.facebook.com/me/friends?fields=id,name,birthday

0

這裏是你在一個幸福的地方所需要的代碼。我必須閱讀大量的文章才能實現這一目標。它的直接複製和粘貼代碼。如果有幫助,請評估它。

在你的*。m文件

#import <FacebookSDK/FacebookSDK.h> 

/****************** 
FACEBOOK 
******************/ 

- (IBAction) facebookActionBtn 
{ 

    // Open session with public_profile (required) and user_birthday read permissions 
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"] 
             allowLoginUI:YES 
            completionHandler: 
    ^(FBSession *session, FBSessionState state, NSError *error) { 
     __block NSString *alertText; 
     __block NSString *alertTitle; 
     if (!error){ 
      // If the session was opened successfully 
      if (state == FBSessionStateOpen) 
      { 
       // Your code here 
       NSLog(@"all good .."); 

       [self getUserFriendsBDays]; 

      } 
      else 
      { 
       // There was an error, handle it 
       if ([FBErrorUtility shouldNotifyUserForError:error] == YES) 
       { 

        alertTitle = @"Something went wrong"; 
        alertText = [FBErrorUtility userMessageForError:error]; 
        [[[UIAlertView alloc] initWithTitle:alertTitle 
               message:alertText 
               delegate:self 
             cancelButtonTitle:@"OK!" 
             otherButtonTitles:nil] show]; 

       } else { 
        // If the user cancelled login 
        if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) 
        { 
         alertTitle = @"Login cancelled"; 
         alertText = @"Your friends birthday will not be entered in our calendar because you didn't grant the permission."; 
         [[[UIAlertView alloc] initWithTitle:alertTitle 
                message:alertText 
                delegate:self 
              cancelButtonTitle:@"OK!" 
              otherButtonTitles:nil] show]; 

        } else { 
         NSLog(@"error 2 .."); 
        } 
       } 
      } 
     } 
    }]; 

} 

- (IBAction) getUserFriendsBDays 
{ 

    NSLog(@"promptUserForFriendsBDays ..."); 




    NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken]; 
    //NSLog(@"permissions::%@ ... fbAccessToken: %@ ...",FBSession.activeSession.permissions, fbAccessToken); 


    NSString *request = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?fields=id,name,birthday&access_token=%@",fbAccessToken]; 
    NSURL *URL = [NSURL URLWithString:request]; 

    NSError *error; 
    NSString *FBOutPutStr = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error]; 
    //NSLog(@"FBOutPutStr: %@ ...", FBOutPutStr); 


    // Convert to JSON object: 
    NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:[FBOutPutStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]; 
    //NSLog(@"jsonObject=%@ ...", jsonObject); 

    NSArray* dataField = [[NSArray alloc] initWithArray:[jsonObject objectForKey:@"data"]]; 
    NSLog(@"dataField=%@ ...", dataField); 


    // Extract values: 
    NSArray *userIDArray = [dataField valueForKey:@"id"]; 
    NSArray *userNameArray = [dataField valueForKey:@"name"]; 
    NSArray *userBdayArray = [dataField valueForKey:@"birthday"]; 

    NSLog(@"userIDArray=%d ... userNameArray=%d ... userBdayArray=%d ...", [userIDArray count],[userNameArray count],[userBdayArray count]); 

} 
相關問題