Xcode, fetch Facebook hometown name - ios

I am beginner in ObjectiveC programming language.
I developing application, using Facebook.
I try to fetch Hometown name.
Now, i want copy "Sveti Martin na Muri" to string to display in tableview.
I use:
FBRequest *friendRequest = [FBRequest requestForMe];
[friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
and output is:
NSLog(#"%#", [result objectForKey:#"hometown"]);
{
id = 107836872573218;
name = "Sveti Martin na Muri";
}
The same problem i have with relationship, work, location and education.
How can I access only name?
Sorry for my bad English.
greetings from Croatia.

The following code:
[result objectForKey:#"hometown"]
returns a NSDictionary with two field id and name.
You can access the name like:
NSLog(#"%#", [[result objectForKey:#"hometown"] objectForKey:#"name"]);

Related

How to get the list of Facebook friends who have not installed the app?

I'm developing a social networking app. I've integrated Facebook SDK 3.14 in my iOS App. Now, I want to get the list of all my Facebook friends so I can invite those friends who are not using the app and send friend requests to those friends who have already installed the app.
I can get the list of friends who already use my apps using "/me/friends".
[FBRequestConnection startWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"All == %#", result);
}];
It gives friends' Facebook ids (ex. id = 654330727444458) in response so that I can send friend requests to them.
To get the list of all Facebook friends who have not downloaded the app and if I want to invite those, I need to get all friends using "me/taggable_friends" (Please correct me if I'm wrong).
[FBRequestConnection startWithGraphPath:#"/me/taggable_friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSlog("%#", result);
}];
In the taggable_friends response I'm getting friend's id as id = "AaLYBzZzHdzCmlytqyMAjO0OLDZIOs74Urn93ikeRmmTB3vX_Xl81OYUt4XnoWG0BDLuX67umVkheVdDjzyvm0fcqMqu84GgM9JnNHc-1B63eg" which is friend's token id and it's not unique. I couldn't use it instead, have to use Facebook Id of friend to invite them. Unfortunately, I couldn't get it in the taggable friend response.
This is only possible on the Facebook API v1 which stops working next April or so. Even now only existing Facebook apps will allow you to use V1 so if you don't have an old enough app you are not able to do this. In V2 you can only get friends who have also signed in to the same app but the user id's are unique to the application to prevent exactly this. I guess Facebook reasons that by doing this is stops people spamming their friends via apps so much :/
As #joelrb says you can create a canvas app for a game and use invitable_friends but FB will vet your "game" so you can't really cheat.
See https://developers.facebook.com/docs/apps/changelog/ for more info.
TLDR; post to wall is all you can do. Tough. Sorry.
Use the installed field of a user. Like so:
NSMutableArray *notYetUsers = [NSMutableArray array];
FBRequest *fbRequest = [FBRequest requestForGraphPath:#"me/friends?fields=installed"];
[fbRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSAssert(!error, error.localizedDescription);
NSArray *friends = [(NSDictionary *)result objectForKey:#"data"];
for (NSDictionary<FBGraphUser> *user in friends) {
if ([user[#"installed"] isEqual:#(NO)])
[notYetUsers addObject:user];
}
}];
notYetUsers would contain all friends who have not installed the app yet.
- (void)getFBFriendsWithCompletion:(void (^)(NSError *, id))callback
{
NSString *query = #"select uid, name, is_app_user "
#"from user "
#"where uid in (select uid2 from friend where uid1=me() )";
NSDictionary *queryParam =
[NSDictionary dictionaryWithObjectsAndKeys:query, #"q", nil];
// Make the API request that uses FQL
[FBRequestConnection startWithGraphPath:#"/fql"
parameters:queryParam
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (callback)
callback(error, result);
}];
}
- (void)foo
{
[self getFBFriendsWithCompletion:^(NSError *error, id result) {
if (!error)
{
NSMutableArray *friendsUsingApp = [NSMutableArray array];
NSMutableArray *friendsNotUsingApp = [NSMutableArray array];
for (NSDictionary *data in result[#"data"]) {
if ([data[#"is_app_user"] boolValue] == NO) {
[friendsNotUsingApp addObject:data];
} else {
[friendsUsingApp addObject:data];
}
}
// Do something with friendsUsingApp and friendsNotUsingApp
}
}];
}
taggable_friends refers to a list of friends that can be tagged or mentioned in stories published to Facebook. The result you got is just a tagging token which can only be used in order to tag a friend, and for no other purpose.
Although this refers to a game app, it's easier I think if you use the invitable_friends API. But it requires a Facebook Canvas app implementation. You may just provide a notice in your Canvas for users to just use the mobile app instead, etc.
This is the tutorial that uses invitable_friends API: https://developers.facebook.com/docs/games/mobile/ios-tutorial/
And, the invitable_friends API details:
https://developers.facebook.com/docs/games/invitable-friends/v2.0
You can try this to get the IDs of Friends app users:
NSMutableArray *appFriendUsers = [[NSMutableArray alloc] init];
[[FBRequest requestForGraphPath:#"me/friends?fields=installed"]
startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary *result,
NSError *error) {
//if result, no errors
if (!error && result)
{
//result dictionary in key "data"
NSArray *allFriendsList = [result objectForKey:#"data"];
if ([allFriendsList count] > 0)
{
// Loop
for (NSDictionary *aFriendData in allFriendsList) {
// Friend installed app?
if ([aFriendData objectForKey:#"installed"]) {
[appFriendUsers addObject: [aFriendData objectForKey:#"id"]];
break;
}
}
}
}
}];

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

Facebook iOS SDK(Graph API) can't access public page information

So I'm trying to get the name of a page with the Facebook graph API using the iOS SDK:
NSDictionary* params = [NSDictionary dictionaryWithObject:#"id,name" forKey:#"fields"];
[[FBRequest requestWithGraphPath:[NSString stringWithFormat:#"%#", userId] parameters:params HTTPMethod:nil] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
name = [result objectForKey:#"name"];
//do more stuff
}];
I'm getting the error
error = {
code = 803;
message = "(#803) Some of the aliases you requested do not exist: (null)";
type = OAuthException;
};
My accesstoken is valid and it does work when I'm using the Graph API Explorer with the GET request 5120148605?fields=id,name,about,bio.
Anyone having an idea of this weird behaviour?
I now resolved this by userId = [NSString stringWithFormat:#"%#", userId]. Seems like I need this for pages but not for users.
I don't know why this is present or if someone else encountered this problem, but that's my way of solving it.

How to get photos of a facebook album in iPhone SDK?

I am developing a iPhone application which get facebook album with this URL https://graph.facebook.com/me/albums?access_token=2227470867|2.ZJr73vaEvFeN4fI_A70RFw__.3600.1297090800-100000566543036|WmgxdwULBgKXl8J7ksGIA1tyGik
Now i want to get photos from this album.
Any Idea will be appreciated.
Thanks in Advance.
So /me/albums returns an array of Album objects, each of which has an id.
If an album had an id of 99394368305, you can go to
https://graph.facebook.com/99394368305/photos
to get an array of photo objects.
Each of these will have the picture and source properties to get the image data from facebook.
They will all have an images array as well if you want pre-scaled images instead of just the originals.
All the facebook queries give JSON back - I'm assuming that you know how to parse this?
It takes lot of time because of following steps;
1) get uid of friend using :
NSString *urlString=[NSString stringWithFormat:#"https://graph.facebook.com/me/friends?access_token=%#&fields=id,picture,name,birthday",fbGraph.accessToken];
2) put friends uid to get album id:
NSString *urlString1=[NSString stringWithFormat:#"https://graph.facebook.com/%#/albums?access_token=%#",obj.uid,fbGraph.accessToken];
NSString *album = [[[_response1 objectForKey:#"data"]objectAtIndex:j]objectForKey:#"name"];
if ([album isEqualToString:#"Profile Pictures"])
{
albumid = [[[_response1 objectForKey:#"data"]objectAtIndex:j]objectForKey:#"id"];
}
3) then put the album id to get the picture from that album:
NSString *albumUrl=[NSString stringWithFormat:#"https://graph.facebook.com/%#/photos?type=album&access_token=%#",albumid,fbGraph.accessToken];
NSString *picUrl=[[[[[_response2 objectForKey:#"data"]objectAtIndex:0]objectForKey:#"images"]objectAtIndex:0]objectForKey:#"source"];
Kindly tell me some other method to get profile picture with large size in a single step to save time.
The code below should do the right job for you. It gets albums for logged user and then requests for photos of this albums. In the place where is a comment you will have the access to all photos. For more info you can refer to docs:
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
}];
}
}
}];
Facebook developers new code:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{user-id}"
parameters:params
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
// Handle the result
}];
Latest Facebook Graph API docs

Resources