ios facebook sdk get and display username - ios

I have been working with the facebook sdk and parse and I am trying to retrieve a users facebook name but the documentation doesn't give me sufficient help and most tutorials on the net are out dated.
Can anyone tell how I can get a users name from the facebook sdk to display as a text label ? This would be greatly appreciated thanks.

You need to implement following FBLoginViewDelegate
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
NSLog(#"user is %# %#", [user objectForKey:#"email"], user.name);
}

You can retrieve it in this way:
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me"];
[request startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary < FBGraphUser > *my,
NSError *error)
{
NSString *username = my[#"username"];
}

I finally figured it out, you have to request the current users profile information than set your label with a "key" which in this case is called "name"
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me"
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"fetched user:%#", result);
_usernameText.text = result[#"name"];
}];

Related

Sorting Through Graph API of Facebook Page on iOS

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
}

Facebook SDK v4 + Parse 1.7.5 issue

I've updated my SDKs from Facebook and Parse to the newest ones which includes some change in the PFFacebookUtils structure.
I would like to create a PFUser with some content from FB. It have been working for a long time before the update, but now when I log in, I only receive the user's name and FBID as result from the Facebook request.
This is how "I" log in:
NSArray *permissionsArray = #[#"public_profile", #"email", #"user_education_history", #"user_work_history", #"user_birthday", #"user_friends", #"user_photos"];
[PFFacebookUtils logInInBackgroundWithReadPermissions:permissionsArray block:^(PFUser *user, NSError *error) { ... }];
This is how I try to get the results:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Result %#", result);
...
}];
The log only shows this:
2015-08-03 01:41:15.039 XXXXXX[10807:2640469] Result {
id = 1015XXXXXXXXXXXX;
name = "Simon XXXX";
}
The Facebook App ID is set in the pList.
The latest Facebook SDK uses the Graph API v2.4, so the special me request needs the specific fields as query parameters to the graph path.
So, if you need to access the first, last name and email, the graph path in your code would look something like this:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me?fields=first_name,last_name,email" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Result %#", result);
...
}];
I also suggest that you check out and play with the Graph API Explorer (note the API version dropdown in the top right corner).

Fetching Facebook user data using fbsdk 4.2

I am a beginner in iOS development. I searched a lot but didn't get my code working cause of some deprecated class in latest fbsdk 4.2. What i want is that integration of fb in my apps. I am Able to integrate fb in my app and i can successfully login and logout. Now my app need is to fetch the logged-in user e-mail id and display it in a label. Here is my code, which i have tried.
-(void)viewDidLoad {
[super viewDidLoad];
if ([FBSDKAccessToken currentAccessToken]) {
self.loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me"
parameters:#{#"fields":#"email"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if(!error)
{
self.lblEmail.text = [result objectForKey:#"email"];
NSLog(#"email:%#", result);
}
}];
}
this is working fine with me for facebook sdk 4.2
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler: ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
NSLog(#"%#",result);
else
NSLog(#"%#",error);
}];
}
Hope this will help you.

Facebook iOS SDK v4.0, get friends list

Seems like Facebook iOS SDK v4.0 has a lot of changes. I try to get a user's friends list after he login.
I think it's something to do with FBSDKGraphRequestConnection but I'm not sure how it can be used.
Also, if I only want to get friends who are using my app, does facebook sdk support that?
Can anyone give some examples? Thanks!
FBSDKGraphRequest *friendsRequest =
[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/friends"
parameters:parameters];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:friendsRequest
completionHandler:^(FBSDKGraphRequestConnection *innerConnection, NSDictionary *result, NSError *error) {
...
}];
// start the actual request
[connection start];
This shows how to handle the response:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/friends" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSArray * friendList = [result objectForKey:#"data"];
for (NSDictionary * friend in friendList)
{
NSLog(#"Friend id: %#", friend[#"id"]);
NSLog(#"Friend name: %#", friend[#"name"]);
}
}
}];
I used below code to get friend list
self.graphrequest = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me/taggable_friends?limit=100"
parameters:#{ #"fields":#"id,name,picture.width(100).height(100)"}];
[self.graphrequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
self.resultsArray = result[#"data"];
[self.fbTableView reloadData];
[self.fbTableView setHidden:NO];
} else {
NSLog(#"Picker loading error:%#",error.userInfo[FBSDKErrorLocalizedDescriptionKey]);
}
}];
You can limit the number of friends using limit keyword.Ex: limit=100

How to retrieve photo album from Facebook in iOS

I want to retrieve all my photo album from my Facebook account. I follow Graph API also and using latest Facebook API.
My code is like this:
-(IBAction)getgallery:(id)sender
{
FBRequest *request = [FBRequest requestForGraphPath:#"me/albums"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary <FBGraphUser> *my, NSError *error) {
if ([my[#"data"]count]) {
for (FBGraphObject *obj in my[#"data"]) {
FBRequest *r = [FBRequest requestForGraphPath:[NSString stringWithFormat:#"/%#/photos", obj[#"id"]]];
[r startWithCompletionHandler:^(FBRequestConnection *c, NSDictionary <FBGraphUser> *m, NSError *err) {
//here you will get pictures for each album
}];
}
}
}];
}
I follow this link. But can't understanding properly. Please anyone can help me?
Nothing is happening when i press the getgallery button.

Resources