i'm trying to retrieve the current users birthday through FBRequestConnection. i've successfully retrieved id, name and gender, but birthday just returns null. Why and what can i do in order to get the current users birthday? do i need some kind of permisson?
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *facebookId = [result objectForKey:#"id"];
NSString *facebookName = [result objectForKey:#"name"];
NSString *birthday = result[#"birthday"];
NSString *gender = result[#"gender"];
NSLog(#"%#", birthday);
}
}];
loginButton = [[FBSDKLoginButton alloc] init];
loginButton.frame = CGRectMake(self.view.frame.size.width/2-loginButton.frame.size.width, self.view.frame.size.height/2+100, 250, 20);
loginButton.readPermissions=#[#"email",#"user_birthday"];
try to add code above.
Related
Hi there I'm dealing with a big issue at least for me, I have implemented an older version of Facebook SDK I have many features in my app using this SDK and updated to the most recent version will take a few days, and I don't have time now to do that so what I want to do is simple, I just want to receive the parameters email, last_name and name but Facebook just returns me the name and the id, so could yo have any idea or I am doing something wrong?. Thanks in advance.
- (void)requestUserInfo{
// We will request the user's public picture and the user's birthday
// These are the permissions we need:
NSArray *permissionsNeeded = #[#"public_profile", #"email"];
// Request the permissions the user currently has
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{
// If there's one, just open the session silently, without showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile", #"email"]
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
{
NSLog(#"%#", session.accessTokenData.accessToken);
}];
}
[FBRequestConnection startWithGraphPath:#"me"
parameters:[NSDictionary dictionaryWithObject:#"id,email,name,last_name" forKey:#"fields"]
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
// Parse the list of existing permissions and extract them for easier use
NSMutableArray *currentPermissions = [[NSMutableArray alloc] init];
NSArray *returnedPermissions = (NSArray *)[result data];
for (NSDictionary *perm in returnedPermissions) {
if ([[perm objectForKey:#"status"] isEqualToString:#"granted"]) {
[currentPermissions addObject:[perm objectForKey:#"permission"]];
}
}
//NSLog(#"Needed: %#", permissionsNeeded);
//NSLog(#"Current: %#", currentPermissions);
NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:permissionsNeeded copyItems:YES];
[requestPermissions removeObjectsInArray:currentPermissions];
//NSLog(#"Asking: %#", requestPermissions);
// If we have permissions to request
if ([requestPermissions count] > 0){
// Ask for the missing permissions
[FBSession.activeSession
requestNewReadPermissions:requestPermissions
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// Permission granted, we can request the user information
[self makeRequestForUserData];
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
NSLog(#"error %#", error.description);
}
}];
} else {
// Permissions are present
// We can request the user information
[self makeRequestForUserData];
}
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
NSLog(#"error %#", error.description);
}
}];
}
And this is the info:
llega el objeto:{
id = 10210924411602727;
name = "Del Mar Sal";
}
Below i attached the FB Login & Fetch details from Facebook. Try it that code & Compare.
- (IBAction)facebook_BtnTap:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"public_profile",#"email"]handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
if ([result.grantedPermissions containsObject:#"email"]) {
// Do work
[self GetUserDetails];
}
}
}];
}
-(void)GetUserDetails
{
BOOL LOGGING;
if (LOGGING) {
return;
}
LOGGING = YES;
// [super startLoader];
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#“Check Already login");
//[FBSession openActiveSessionWithAllowLoginUI: YES];
// [self back:nil];
// return;
}
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:#"id,name,email,first_name,last_name,birthday,picture.type(large),location,hometown,gender" forKey:#"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString* email = [result objectForKey:#"email"];
NSString* fname = [result objectForKey:#"first_name"];
NSString* lname = [result objectForKey:#"last_name"];
NSString* userName = [result objectForKey:#"name"];
NSString* gender = [result objectForKey:#"gender"];
NSDictionary* hometown=[result objectForKey:#"hometown"];
NSString* country = [hometown objectForKey:#"name"];
NSString* birthday=[result objectForKey:#"birthday"];
// NSArray* photo =[[result objectForKey:#"picture"]valueForKey:#"data"];
// NSDictionary* location = [result objectForKey:#"location"];
// NSString* city = [location valueForKey:#"name"];
// NSString* fbPhoto = [photo valueForKey:#"url"];
NSString* fid = [result objectForKey:#"id"];
}];
}
I am using:
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
This gives me a JSON string of ALL data (AND I MEAN ALL) from a Facebook Page. It gives me the posts, IDs of those who liked the posts, every comment, every person who shared. I really just need the post itself, which is listed as 'message' in the JSON result. Is there a way I can do this in the API call, or does it have to be done after?
Also,
Is there any way to get it to pull the pictures that are associated with each post? I know how to get photos posted to page, but I just want to view the posts made to the page, and have it also pull up the picture.
You can filter out feed response as below
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:[NSMutableDictionary dictionaryWithObject:#"id, message, link.picture" forKey:#"fields"]]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
As you can check, I've mentioned the parameters which I information I requires to get filter out.
NOTE: You can filter out things according to your requirement.
Please check below link for available filter possibilities on facebook SDK.
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
I hope it will help you, Not sure about the picture you want to pull up but may be 'link.picture' in 'fields' will help you the picture you want to get.
Use
/{page_id}/feed?fields=id,message
With Graph API v2.4 this will be the standard usage that you have to specify each field.
this, it's the variable of NSString "link" that you want:
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *link = userData[#"link"];
NSString *locale = userData[#"locale"];
NSString *timezone = userData[#"timezone"];
NSString *last_name = userData[#"last_name"];
NSString *email = userData[#"email"];
NSString *gender = userData[#"gender"];
NSString *first_name = userData[#"first_name"];
} else if (error) {
//tbd
}
}];
} else if (![FBSDKAccessToken currentAccessToken]) {
//tbd
}
I am making in app in which i show user's hometown,location on profile screen. I get the required data when using developer's account and get data for every user when current user is looking at its own profile.
But when i try to look at other's profile, i don't get the desired data. Here is my code for how i am getting the data out.
-(void)loadFbData{
NSMutableString *graphPath = [NSMutableString stringWithFormat:#"/?id=%#",[self.user valueForKey:#"facebookId"]];
[graphPath appendString:#"&fields=location,hometown,picture.width(640).length(342),name,gender,email,website"];
[FBRequestConnection startWithGraphPath:graphPath completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.personalDataView.webLabel.text = [result objectForKey:#"website"];
self.personalDataView.emailLabel.text = [result objectForKey:#"email"];
self.personalDataView.webLabel.text = [result valueForKey:#"website"];
self.profileBasicDataView.livesInLbl.text = [[result objectForKey:#"location"] objectForKey:#"name"];
self.profileBasicDataView.fromLbl.text = [[result objectForKey:#"hometown"] objectForKey:#"name"];
});
NSURL *url = [NSURL URLWithString:[[result objectForKey:#"picture"] valueForKeyPath:#"data.url"]];
[self.profileBasicDataView loadUserFbData:result];
[self.profileImage loadImage:url];
}
}];
}
**website and email is hidden when other user's profile is shown.
I need to get profile picture from profile album only.
I know how to get profile picture directly from userId and i know how to get the photos of album.
the problem is -
I need to get all the photos from profile album and need to set the profile picture on first position.
If i get the profile picture directly from userId and after that i place the profile album photos then the profile picture will repeat twice because it's in album too.
So, i need some solution like i'll identify the profile picture from profile album.
any link,tutorial,suggestion,idea will be great help.
Update
This will definitely help you.
[FBRequestConnection startWithGraphPath:#"me?fields=albums.fields(name,photos.fields(name,picture,source))"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
//NSLog(#"result %#",result);
NSString * anImage;
NSArray* albums = result[#"albums"][#"data"];
NSUInteger index = [albums indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj[#"name"] isEqualToString: #"Profile Pictures"];
}];
if (index != NSNotFound) {
NSDictionary *profileImages = albums[index];
NSDictionary *photos = profileImages[#"photos"];
NSArray *data = photos[#"data"];
for (NSDictionary *picture in data) {
NSString *date = picture[#"created_time"];
NSString *id = picture[#"id"];
NSString *pictureUrl = picture[#"picture"];
NSString *sourceUrl = picture[#"source"];
}
}
}];
The reply above (below once I added my reply) helps me. I upvote, the updated solution for SDK V4:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"albums.fields(name,photos.fields(name,picture,source,created_time))"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//NSLog(#"result %#",result);
NSString * anImage;
NSArray* albums = result[#"albums"][#"data"];
NSUInteger index = [albums indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj[#"name"] isEqualToString: #"Profile Pictures"];
}];
if (index != NSNotFound) {
NSDictionary *profileImages = albums[index];
NSDictionary *photos = profileImages[#"photos"];
NSArray *data = photos[#"data"];
for (NSDictionary *picture in data) {
NSString *date = picture[#"created_time"];
NSString *id = picture[#"id"];
NSString *pictureUrl = picture[#"picture"];
NSString *sourceUrl = picture[#"source"];
}
}
}];
I'm using Facebook SDK to get user's details with permissions:
[NSArray arrayWithObjects:#"user_photos",#"public_profile",#"user_location", #"user_birthday",nil];
I know how to get the city but how can I get user's country?
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *name = userData[#"name"];
NSString *city = userData[#"location"][#"name"];
}
else
{
//...
}
}];
It should be [#"location"][#"country"] as #trick14 outlined. If you can't get a value from it, it can have multiple reasons:
The user you're querying has no location information in his profile
The Access Token you're using has no granted user_location permission (Check with a call to /me/permissions)
There's no country info for the location (quite unlikely)
You have a problem with your permissions string BTW, user_location is contained twice, and offline_access and publish_stream are deprecated long ago.
try this:
NSString *city = userData[#"location"][#"city"];
See here for more info.. :)