Getting basic facebook profile information ios - ios

I have logged in on my iOS app that I am building using the easier method of FBSDKLoginButton.
Since all I want is the users name and a link to their facebook I know I can find this under [FBSDKProfile currentprofile].
My question really is how do I use this?
I am trying all sorts of combinations but something akin to the below is an example of what I'm doing.
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
NSString *name = [[NSString alloc]init];
name = [[FBSDKProfile currentProfile]firstName];
I figure since my app now has access which I quickly tested through an if on the [FBSDKAccessToken currentAccessToken] so I believe I am primed. My app delegate is all setup also. So shouldn't my above code just work? I mean, I assume currentProfile is set if a user is logged in? Maybe? Arghhh!!!
Thanks guys

Try this code :
FBRequestHandler completionBlock = ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
NSLog(#"USER DICTIONARY : %#", user);
};
[[FBRequest requestForMe] startWithCompletionHandler:completionBlock];
you can then get the user details from the user dictionary.

I sorted it guys. Essentially the FBSDKLoginButton logs you in but offers no permissions. Simply ask for them as so;
loginButton.readPermissions = #[#"email", #"public_profile"];
Then the normal code I tried in my above comment will work. My mistake here is I assumed a simple login would at least give me the public profile. It doesn't, you have to ask.

Related

iOS Facebook SDK: FBSDKGraphRequest not getting email, despite permission

I am using FB SDK v4.4 (latest) and think I have avoided the gotchas in the other questions.
I am establishing a logIn:
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else {
[self facebookGetUserProfileWithCompletion:completion];
}
}];
I've specifically asked for "email" (an extended permission that requires app review).
My app has passed review, and the user gives permission for email when prompted. Adding a [result.grantedPermissions containsObject:#"email"] check in the handler returns TRUE.
Once the user's has responded to the UI, the code then gets the user profile from Graph API:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields" : #"id,email,first_name,last_name,link,locale"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *email = [result objectForKey:#"email"] ;
NSString *first_name = [result objectForKey:#"first_name"] ;
//etc...
}
At this point, I have just five parameters; I am missing email. Using Graph API Explorer, I get the full six. The user does have an email address registered with FB.
Why no email???!!!
I've pretty much resolved my issues. Here they are:
1) #cbroe had the most helpful suggestion of getting the token and using Graph API Explorer to debug. Lots of help there.
2) FB are shifting their DOB field to age_range in most cases. It seems that my existing app will still return DOB, but the new version will not without permission. No doc on this I could find, but if I go with age_range, I'm good.
3) There was a strange problem with my email address in my FB test account. That's fixed, and I am getting email again no problem. Again, the Graph API test was most helpful in resolving this. Thanks again #cbroe!
Your parameters should be just one string
#"fields:id,email,first_name,last_name,link,locale"

FB Graph Subscription IOS send access_token

There is something to the Facebook Graph SDK (and its iOS implementation) that really makes my life miserable. And it seems I'm on my own, because I don't seem to be able to find anyone on the 'net with a similar problem/lack of understanding like me.
Without further ado:
I have this code in my iOS app, that is supposed to subscribe my app to user updates:
-(void)subscribeToFacebook{
(FBSession.activeSession.isOpen) ? NSLog(#"is open") : NSLog(#"is closed");
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"page", #"object",
#"http://www.example.com/fbcallback.php", #"callback_url",
#"about, picture", #"fields",
#"ItsMeAlright", #"verify_token",
nil
];
/* make the API call */
[FBRequestConnection startWithGraphPath:#"/app/subscriptions"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"error= %#",error);
}];
}
When I run this code, an error is logged, stating:
"(#15) This method must be called with an app access_token."
Now, I (think) I know how to get the access_token
NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
but I have't got a clue on where and when to send it to Facebook.
I tried to append it to the #"/app/subscriptions" part like this
NSString *urlString = [NSString
stringWithFormat:#"/app/subscriptions?access_token=%#",
[fbAccessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
but that didn't work.
Your input is extremely welcome!
The way to do this will be to create a FBSession with the application token. Because the current session isn't what you want.
https://developers.facebook.com/docs/reference/ios/current/class/FBSessionTokenCachingStrategy
https://developers.facebook.com/docs/facebook-login/ios#sessions
But, security wise, you shouldn't be doing this at all. It's very insecure to embed your application token in a production application. I believe that the iOS sample generated for you is incorrect.
You should be sending any request that requires the application token with a server call (e.g. PHP)

Facebook request for friends returns 0 friends when session is open

I am building an iOS application that is connected to Facebook. I got most of the Facebook related code (pretty much copied all of it) from the TokenCacheHowTo project. I have run into two problems, one which had a simple fix, but I think may be causing my second problem.
At first the login wasn't persisting. I had copied all the code from TokenCacheHowTo, so I was confused why and looked for a solution. Eventually I found this question and it said that if I removed #[#"basic_info"] from the permissions parameter it would work. And it did. I am still confused as to why it would work. Note: I removed #[#"basic_info"] from the openSessionWithAllowLoginUI method in my app delegate.
My problem now is that I want a list of the users friends, so I do the following request in the app delegate.
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSLog(#"Under");
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
}];
This code outputs Found: 0 friends within my app delegate, but if I put the code in the same place in TokenCacheHowTo it outputs Found: X friends where X is my actual number of friends. This is confusing to me because I am able to get the users information such as their id and name from Facebook and store it on my server.
I realize that there is probably a simple solution to this but I don't have a good grasp of the Facebook SDK yet and I'm stuck.
Try this as working fine for me and Ensure about the Permissions like
NSArray *array = [NSArray arrayWithObjects:#"user_friends", nil];
and then fetch all the friend
-(void)getFriendList {
FBRequest* friendsRequest = [FBRequest requestWithGraphPath:#"me?fields=friends.fields(first_name,last_name)" parameters:nil HTTPMethod:#"GET"];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSDictionary* friendsDic = [result objectForKey:#"friends"];
NSArray *friends = [friendsDic objectForKey:#"data"];
for (NSDictionary *friend in friends) {
NSLog(#"firstname %#",friend[#"first_name"]);
NSLog(#"last name %#",friend[#"last_name"]);
}
}];
}
Nothing wrong with your code. I've used the same code which you used above and I got x friends list. But my permission is declared as below
FBSession.activeSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:#"user_about_me", #"email",#"user_birthday", nil]];
Note: I want email id and birthday from users that why my permissions include that fields. And It should be declared before authentication, then only sessions kept this permission as it is.

Return Facebook user info in local language

I'm developing a small app with Xcode that logs into Facebook and retrieves user informations. This is the part of code that performs the request:
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
// at this point user variable contains all user's info I need
NSLog (#"%#", user.name);
} else {
NSLog(#"ERRORE");
}
}];
These infos are returned in English. How can I retrieve them in the same language of Facebook profile ? For example, since the language I choose for my Facebook profile is Italian, I need to retrieve all my infos in italian (with this code user[#"relationship_status"] returns "married", it should return "sposato")
Thanks to this post I found out (a possible) solution: run a FQL query with the locale parameter set to the desired language. In my case it should be locale=it_IT, as stated by this table

How to handle session in facebook ios sdk

Hi I'm working on an app. In the app is possible to sing-in with facebook or not... And in several places of the app you can share stuff on you own timeline.
I followed the scrumptious example that facebook provides, and the log-in is working fine... But when I have to share stuff, I don't know if it's necessary or not to check if the session is active before calling the FBRequestConnection method to post to fb.
What's the best way to handle this? Because all the authentication is made calling an open session method in the delegate so... I have to call this methods from anywhere in the app? How do I go back to the place where I was before calling the authentication method... I'm sure there must be some kind of patron to use, but I can't find which one...
Any idea?
thanks!
Use a block of code like this to make sure that the session is open, which is important because if the user isn't, your app will crash. And then you need to have you OpenGraph objects within the FBRequestConnection.
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
self.nameString = user.name;
self.profileImage.profileID = user.id;
self.userName = user.username;
[self.tableView reloadData];
}
}];
}
}
** For your comment below: **
Okay, then perhaps bring up a UIAlertView asking the user to log into Facebook. If they select yes, then do something like
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSessionWithAllowLoginUI:YES];
assuming that your AppDelegate looks like Scrumptious.

Resources