I am currently trying to load a user's Facebook friends into an NSMutableArray. However, when I use [users addObject: friend.name]; it doesn't add it to the array outside of the block of code. What am I missing here? Also I am trying to receive the number of friends and this also isn't working outside the block of code. What am I doing wrong here?
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
users = [[NSMutableArray alloc] init];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
[users addObject:friend.name];
NSLog(#"%#",users);
}
}];
Full code here: http://pastie.org/private/elynlgrkctakioi8pmtta.
yes it is added in the array, but we have to wait sometime to complete the block process. I am fetching friends and storing in NSMutableArray. and use it after some delay of time. I am using below code.
[FBSession setActiveSession:[self appDelegate].session];
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
users = [[NSMutableArray alloc] init];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends)
{
NSLog(#"name : %#",[friend name]);
[tempArray addObject:friend];
}
}];
[self performSelector:#selector(afterFetchingFbFriend:) withObject:tempArray afterDelay:10.0];
in 10.0 delay it completes the block process and i have all the friends in my array.
and then my method to process array is :
-(void)afterFetchingFbFriend:(NSMutableArray *)tempArray
{
//do something.
}
if not getting in 10.0 delay time then increase it as it depends on internet speed so the fetching friends time may be different. It works for me.
Related
I am trying to use Facebook's iOS SDK and load a list of friends in a UITableView. When I put the following code in viewDidLoad it seemingly doesn't get added to the UITableView. (Where users is the NSMutableArray)
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
[users addObject:friend.name];
}
}];
I've tried adding:
[friendsTableView beginUpdates];
[friendsTableView insertRowsAtIndexPaths:users withRowAnimation:UITableViewRowAnimationRight];
[friendsTableView endUpdates];
To update the UITableView after the request finishes with no change.
What can I do either load the UITableView after the request or update the UITableView when the request finishes?
you cannot just pass any array to insertRowsAtIndexPaths:withRowAnimation:. It expects an array filled with index paths — hence the name.
what you want is to reload the tableview once the response is received.
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
[users addObject:friend.name];
}
[friendsTableView reloadData]; //<------
}];
while tableviews datasource & delegate methods use users as source.
i am getting null in friends list this is my code
-(void)getFriendList
{
FBRequest *friendsRequest=[FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection *connection,NSDictionary* result,NSError *error)
{
arrFriendFB = [result objectForKey:#"data"];
arrFriendFB=[arrFriendFB valueForKey:#"first_name"];
NSLog(#"friemd=%#",arrFriendFB);
NSLog(#"friends description :%#",[arrFriendFB description]);
}];
}
As new facebook APi doc there is some modification. see some change's mention below
first you need to get access token with "user_friends" permission.
its graph API url:-
https://graph.facebook.com/me/friends?access_token=
For more information follow this link
https://developers.facebook.com/docs/facebook-login/permissions/v2.0
use this method to get friend list.
-(void)getFriendList
{
[FBSession setActiveSession:[self appDelegate].session];
FBRequest *friendRequest = [FBRequest requestForGraphPath:#"me/friends?fields=name,picture,location"];
[ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSArray *data = [result objectForKey:#"data"];
//NSLog(#"response : %#",data);
for (FBGraphObject<FBGraphUser> *friend in data)
{
NSLog(#"name : %#",[friend name]);
}
}];
}
I want to access friend list from facebook account,
the proble is that the friend list is accessed in simulatore but not in device, so what i have to do about this??
Thanks...
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
friendsRequest.session = FBSession.activeSession;
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %lu friends", (unsigned long)friends.count);
for (NSDictionary<FBGraphUser>* friend in friends)
{
NSLog(#"I have a friend named %# ", friend.name);
}
}];
Try this..
#property (nonatomic, strong) FBRequestConnection *connection;
-(void)requestAllFreindsWithHandler:(FBRequestHandler)handler;
{
NSAssert([FBSession activeSession] != nil, #"Login required ");
self.connection = [FBRequestConnection startForMyFriendsWithCompletionHandler:handler];
}
I'm trying to get the user's friend list with iOS Facebook SDK 3.1 but I'm getting an empty list as a result. This is what I'm doing:
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"NAME %#", friend.name);
}
}];
I'm getting this:
{
data = (
); }
This app has also Facebook Login and it works OK. What am I missing?
Thanks in advance!
I am trying to get a list of all my Facebook Friends, who are using the App I am getting the list of all friends, but how do I filter all the friends which are using the app?
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
NSArray *friendIDs = [friends collect:^id(NSDictionary<FBGraphUser>* friend) {
return friend.id;
}];
}];
Thanks.
Thats the way how it works with iOS5+
FBRequest* friendsRequest = [FBRequest requestWithGraphPath:#"me/friends?fields=installed" parameters:nil HTTPMethod:#"GET"];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
NSArray *friendIDs = [friends collect:^id(NSDictionary<FBGraphUser>* friend) {
return friend.id;
}];
}];
This can be used for latest Facebook API 3.2 ,
[FBRequestConnection startForMyFriendsWithCompletionHandler:
^(FBRequestConnection *connection, id<FBGraphUser> friends, NSError *error)
{
if(!error){
NSLog(#"results = %#", friends);
}
}
];
"is_app_user" that you need to check.
- (void)getFriends
{
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 (error) {
DLog(#"Error: %#", [error localizedDescription]);
[self.delegate FBDidFailedLoadFriends:error];
} else {
DLog(#"Result: %#", result);
}
DLog(#"%#",((FBGraphObject*)result)[#"data"]);
}];
}
You can do it with FQL. It will directly give list of friends who are using app.
SELECT uid FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ?)
AND is_app_user = 1
Should be closed as Facebook has changed the way you can access the friends list.
Now all you can access is app users.