I am using iOS sdk v3.18.1, I want to get all my Facebook friends.I can get the friends count but, data is nil.
Here is my code
[FBRequestConnection startWithGraphPath:#"me/friends" parameters:nil HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"result %#",result);
}];
Out put
{
data = (
);
summary = {
"total_count" = 840;
};
}
https://developers.facebook.com/docs/apps/changelog
Since v2.0 you cannot get the full friend list anymore, you only get the friends who authorized your App too.
See my answer in this thread too: how to get a list of all user friends (not only who use the app)?
// declare an array in header file which will hold the list of all friends -
NSMutableArray * m_allFriends;
// alloc and initialize the array only once
m_allFriends = [[NSMutableArray alloc] init];
With FB SDK 3.0 and API Version above 2.0 you need to call below function (graph api with me/friends)to get list of FB Friends which uses the same app.
// get friends which use the app
-(void) getMineFriends
{
[FBRequestConnection startWithGraphPath:#"me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"me/friends result=%#",result);
NSLog(#"me/friends error = %#", error.description);
NSArray *friendList = [result objectForKey:#"data"];
[m_allFriends addObjectsFromArray: friendList];
}];
}
Note : 1) The default limit for the number of friends returned by above query is 25. 2)If the next link comes in result, that means there are some more friends which you will be fetching in next query and so on. 3)Alternatively you can change the limit (reduce the limit, exceed the limit from 25) and pass that in param.
////////////////////////////////////////////////////////////////////////
For non app friends -
// m_invitableFriends - global array which will hold the list of invitable friends
Also to get non app friends you need to use (/me/invitable_friends) as below -
- (void) getAllInvitableFriends
{
NSMutableArray *tempFriendsList = [[NSMutableArray alloc] init];
NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:#"100", #"limit", nil];
[self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}
- (void) getAllInvitableFriendsFromFB:(NSDictionary*)parameters
addInList:(NSMutableArray *)tempFriendsList
{
[FBRequestConnection startWithGraphPath:#"/me/invitable_friends"
parameters:parameters
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"error=%#",error);
NSLog(#"result=%#",result);
NSArray *friendArray = [result objectForKey:#"data"];
[tempFriendsList addObjectsFromArray:friendArray];
NSDictionary *paging = [result objectForKey:#"paging"];
NSString *next = nil;
next = [paging objectForKey:#"next"];
if(next != nil)
{
NSDictionary *cursor = [paging objectForKey:#"cursors"];
NSString *after = [cursor objectForKey:#"after"];
//NSString *before = [cursor objectForKey:#"before"];
NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:
#"100", #"limit", after, #"after"
, nil
];
[self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}
else
{
[self replaceGlobalListWithRecentData:tempFriendsList];
}
}];
}
- (void) replaceGlobalListWithRecentData:(NSMutableArray *)tempFriendsList
{
// replace global from received list
[m_invitableFriends removeAllObjects];
[m_invitableFriends addObjectsFromArray:tempFriendsList];
//NSLog(#"friendsList = %d", [m_invitableFriends count]);
[tempFriendsList release];
}
For Inviting non app friend -
you will get invite tokens with the list of friends returned by me/invitable_friends graph api. You can use these invite tokens with FBWebDialogs to send invite to friends as below
- (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
userInviteTokens, #"to",
nil, #"object_id",
#"send", #"action_type",
actionLinksStr, #"actions",
nil];
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:#"Hi friend, I am playing game. Come and play this awesome game with me."
title:nil
parameters:params
handler:^(
FBWebDialogResult result,
NSURL *url,
NSError *error)
{
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request : %#", error.description);
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
// User clicked the "x" icon
NSLog(#"User canceled request.");
NSLog(#"Friend post dialog not complete, error: %#", error.description);
}
else
{
NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];
if (![resultParams valueForKey:#"request"])
{
// User clicked the Cancel button
NSLog(#"User canceled request.");
}
else
{
NSString *requestID = [resultParams valueForKey:#"request"];
// here you will get the fb id of the friend you invited,
// you can use this id to reward the sender when receiver accepts the request
NSLog(#"Feed post ID: %#", requestID);
NSLog(#"Friend post dialog complete: %#", url);
}
}
}
}];
}
Since, V2.0 of the Graph API, You will only be able to get the list of the friends who are connected with your app. In v2.0 of the Graph API, calling /me/friends returns the person's friends who use the app. Yes it is possible to get the count but accessing the friend list is not possible.
All the release of Facebook SDK after the month of April denies this functionality of getting the whole list of friends.
REFER : SO QUESTION:Facebook graph API returns empty .... FACEBOOK USER GUIDE
This has been confirmed by FACEBOOK.
Related
I'm implementing deep linking in an app.
Followed every tutorial I found.
Tested every possible setting.
And the result is: If I post a story with "user_generated": "true", Facebook posts the big photo as it should, but there is no link like "view on Instagram", "view on Foursqare" and so on. So people can't reach the actual object.
Facebook settings:
Display Name, Namespace, App Domains, Site URL, Bundle ID, iPhone Store ID, Deep Linking (ON), Native or desktop app?(ON), Is your App Secret embedded?(OFF)
App Restrictions - No restrictions
I'v set up the open graph action and object and they are working without "user_generated": "true".
I'm sending my code (projectnamespace is replaced for this example):
-(void) shareOnFacebook: (NSString *) image {
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
object[#"type"] = #"projectnamespace:photo";
object[#"title"] = _postText.text;
object[#"url"] = responseJson[#"share"];
object[#"og:url"] = responseJson[#"share"];
object[#"al:web"] = responseJson[#"share"];
object[#"web"] = responseJson[#"share"];
if (image) {
NSLog(#"Photo Link: %#", image);
object[#"image"] = #[#{#"url": image, #"user_generated" : #"true" }];
}
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *objectId = [result objectForKey:#"id"];
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"photo"];
[action setObject: #"true" forKey: #"fb:explicitly_shared"];
if (image)
[action setImage:#[#{#"url": image, #"user_generated" : #"true" }]];
// create action referencing user owned object
[FBRequestConnection startForPostWithGraphPath:#"me/projectnamespace:add" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *actionId = [result objectForKey:#"id"];
NSLog(#"Posted story to facebook: %#", actionId);
[self shareAndDismiss: nil];
} else {
// An error occurred
NSLog(#"Encountered an error posting action to Open Graph: %#", error);
[self shareAndDismiss: nil];
}
}];
} else {
// An error occurred
NSLog(#"Error posting the Open Graph object to the Object API: %#", error);
[self shareAndDismiss: nil];
}
}];
}
As far as I have seen, there is no such "View on Foursquare" link on Foursquare posts being shared on Facebook.
The only "View on..." links that I am aware of are for Instagram. However Instagram is a Facebook company. The Facebook and Instagram teams work together for their own photo-sharing experience, which means they are not limited by the API as we are.
I'm afraid that "View on Instagram" is a special feature given to the Instagram app only and that the Facebook API doesn't give that ability to others apps.
I want to post highscores from my iOS app on Facebook. I've already read on the Scores API but it does explain to my understanding how exactly my highscore (say int score variable) will be linked programmatically from my code to Facebook. Also I followed the instructions below:
and was getting the following error.
I will really appreciate some help. Thanks.
UPDATED:
I am using the following code to update the score on Facebook:
-(void)postScoreToFacebook:(int)currentScore
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%#", #"4356"], #"score",
nil];
NSLog(#"Fetching current score");
// Get the score, and only send the updated score if it's highter
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/scores", #"XXXXX"] parameters:params HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (result && !error) {
int nCurrentScore = [[[[result objectForKey:#"data"] objectAtIndex:0] objectForKey:#"score"] intValue];
NSLog(#"Current score is %d", nCurrentScore);
if (currentScore > nCurrentScore) {
NSLog(#"Posting new score of %d", currentScore);
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/scores", #"XXXXX"] parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Score posted");
}];
}
else {
NSLog(#"Existing score is higher - not posting new score");
}
}
}];
}
I keep getting the following error when checking the score using the Graph API /USER_ID/scores
{
"error": {
"message": "Unsupported post request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
"type": "GraphMethodException",
"code": 100
}
}
You should be using the user ID of the user whose score you want to update, not the string USER_ID
I want to get facebookid of my friends. But seems like its sending different id's
The situation is i have a friends name "abc" when i loads the friends list then i am getting following response for friend "abc" from facebook
<__NSCFArray 0xce76290>(
{
id = 668201987254085;
name = "abc";
}
)
and my facebook id is "123764734754"returned by facebook
i am using below code for this
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
SLRequest *friendsListRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:URLIFY(#"https://graph.facebook.com/me/friends")
parameters:nil];
friendsListRequest.account = facebookAccount;
[friendsListRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSLog(#"Got a response: %#", [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding]);
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSError *jsonError = nil;
NSDictionary *friendsListData = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
if (jsonError) {
NSLog(#"Error parsing friends list: %#", jsonError);
} else {
self.userList = friendsListData[#"data"];
}
} else {
NSLog(#"HTTP %ld returned", (long)urlResponse.statusCode);
}
} else {
NSLog(#"ERROR Connecting");
}
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
}];
But when i am logging in using user name "abc" then for abc (current login user) i am getting different id say "10008772786278"
same thing when i login then i got my facebook id"123764734754" but when my friend "abc" login then in his friends list response they got my id "897349579554"
why this thing happen? how to get same facebook id of the user that is returned after when he login
Please Try this code.
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error){
NSLog(#"Error :%#",error);
if (session.isOpen && !error) {
[FBRequestConnection startWithGraphPath:#"me/friends" parameters:nil HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection,id result, NSError *error){
if (!error) {
NSLog(#"result %#",result);
[array_firendList addObjectsFromArray:[result valueForKey:#"data"]];
NSLog(#"count %ld",(unsigned long)[array_firendList count]);
}
}];
Facebook recently changed a lot of stuff, see here: https://developers.facebook.com/docs/apps/changelog
See "App-scoped User IDs", you don´t get the real IDs anymore:
To better protect people's information, when people log into a version
of your app that has been upgraded to use Graph API v2.0, Facebook
will now issue an app-scoped ID rather than that person's orginal ID
Also important:
The /me/friends endpoint no longer includes the full list of a
person's friends. Instead, it now returns the list of that person's
friends who are also using your app.
To make sure you always get an App Scoped ID, use /v2.0/{object} instead of /{object}. For example, to get the ID of the logged in user you can use /v2.0/me, for getting the friend list you can use /v2.0/me/friends. This is only necessary if your App is created before May 2014, if you created it later it will only use v2.0 anyway.
Another hint: make sure you get the IDs (with /me or /me/friends) on the same platform and of course with the same App, don´t compare the IDs you get with the Graph API Explorer (for example) with the ones you get in 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;
}
}
}
}
}];
I'm using request dialog in mobile app. Reading
https://developers.facebook.com/docs/reference/dialogs/requests/
I've found that
Note: the filters option is disabled on mobile dialogs and will not affect the set of users that appear in the dialog.
Unfortunately I need to show only friends with app installed in one dialog, and rest in the other. Is there any reasonable way to filter users like this while still using FB request dialog?
You could take a look at the Hackbook sample app, packaged with the SDK to show you how to do this, but in essence you have to create an API call to get, say users with the app installed and pass this to a parameter called "suggestions".
- (void)sendRequest:(NSArray *) targeted {
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"It's your turn, quit slacking.", #"message",
nil];
// Filter and only show targeted friends
if (targeted != nil && [targeted count] > 0) {
NSString *selectIDsStr = [targeted componentsJoinedByString:#","];
[params setObject:selectIDsStr forKey:#"suggestions"];
}
[self.facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
}
- (void) sendToAppUsers {
FBRequest *appUsersRequest = [[FBRequest alloc]
initWithSession:FBSession.activeSession
restMethod:#"friends.getAppUsers"
parameters:nil
HTTPMethod:#"GET"];
FBRequestConnection *appUsersConnection = [[FBRequestConnection alloc] init];
[appUsersConnection
addRequest:appUsersRequest
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// Process the results
NSMutableArray *friendsWithApp = [[NSMutableArray alloc] init];
// Many results
if ([result isKindOfClass:[NSArray class]]) {
[friendsWithApp addObjectsFromArray:result];
} else if ([result isKindOfClass:[NSDecimalNumber class]]) {
[friendsWithApp addObject: [result stringValue]];
}
// User has friends that pass this filter
if ([friendsWithApp count] > 0) {
[self sendRequest:friendsWithApp];
}
}];
[appUsersConnection start];
}