Reputation: 2539
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?
Upvotes: 2
Views: 1303
Reputation: 871
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)
Upvotes: 0
Reputation: 18875
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.
Upvotes: 1
Reputation: 46543
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.
Upvotes: 2