2

I am using Facebook SDK for getting the user birth dates. The birth dates are coming fine as long as user has set the birth date publicly accessible. My issue is that the date settings for Facebook user are different. For example a user can show only date/month, month/year or the whole date or turn off the whole birth date. So the date formats are different. but to convert the date string to NSDate object I have to use NSDateformatter. But by using

[df setDateFormat:@"yyyy/mm/dd"];

for every format the date comes out to be null if there is no year or no month.

So the question is Is there a generic way of converting any date format with a generic dateformmatter? Or can we detect that a string has got months or years or day?

halfer
  • 19,824
  • 17
  • 99
  • 186
Aleem Ahmad
  • 2,469
  • 4
  • 24
  • 38

3 Answers3

2

You can check if the string has year added on it or not.

Then use [df setDateFormat:@"MM/dd"]; or [df setDateFormat:@"yyyy/MM/dd"]; accordingly or any other format the date is coming.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

I checked this one.

On my Facebook profile I set birthday appearance to "day and month only"

In my app i login with following permissions:

NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_birthday",
                                                        @"email",
                                                        @"user_about_me",
                                                        @"user_hometown",
                                                        nil];

FBSession *session = [[FBSession alloc] initWithAppID:nil
                                           permissions:permissions
                                       urlSchemeSuffix:nil
                                     tokenCacheStrategy:_tokenCaching];

Of course you only need @"user_birthday" permission.

Then I have fetchUserData method:

- (void)fetchUserData
{
    if (FBSession.activeSession.isOpen)
    {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error)
         {
             if (!error)
             {
                 NSLog (@"FB:          id: %@", user.id);
                 NSLog (@"FB:    username: %@", user.username);
                 NSLog (@"FB:        link: %@", user.link);
                 NSLog (@"FB:        name: %@", user.name);
                 NSLog (@"FB:  first_name: %@", user.first_name);
                 NSLog (@"FB:   last_name: %@", user.last_name);
                 NSLog (@"FB: middle_name: %@", user.middle_name);
                 NSLog (@"FB:    birthday: %@", user.birthday);
                 NSLog (@"FB:       email: %@",[user objectForKey:@"email"]);
                 NSLog (@"FB:      gender: %@",[user objectForKey:@"gender"]);
             }
         }];
    }
}

And even though I'd set "day and month only" the result in fetchUserData is:

FB:    birthday: 08/16/1978

EDIT:

This works for the logged-in user only. For his friends you will only be able to get the data they allow to post on timeline (even with FQL).

You can try what an FQL query returns in Graph API Explorer (FQL Query)

Make sure to get access token with users friends_birthday permission first (via Get Access token button)

run the query:

SELECT uid, name, birthday_date from user where uid IN (SELECT uid2 FROM friend WHERE uid1=your_facebook_id_here)

If you check the results you'll se that possible formats are:

full birthday: MM/DD/YYYY month and day: MM/DD don't show : <null>

So you'll be best off following advice from Anoop Vaidya.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • Yes, you are right about that. But I am fetching my friends birth dates on facebook. And is they turn only month and date on then the date would come like this 08/27. I am not fetching my birthdate I am fetching my friends birth dates. Apology if I have not mentioned this in the question. – Aleem Ahmad Apr 24 '13 at 04:33
  • I see. That is true and due to privacy policy it's not possible to get a friends full birthday if one does not allow it. – Rok Jarc Apr 24 '13 at 08:46
  • 1
    Thanks a lot for the help. And the answer of anoop is what I am seeing is the best option for me. – Aleem Ahmad Apr 24 '13 at 10:02
0

According to the new Graph API:

According to new Graph API The person's birthday. This is a fixed format string, like MM/DD/YYYY. However, people can control who can see the year they were born separately from the month and day so this string can be only the year (YYYY) or the month + day (MM/DD)

MarBVI
  • 811
  • 2
  • 12
  • 34