Wait for FB request before UITableView loads - ios

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.

Related

Facebook SDk3 how can i get the Facebook all friend list in my tableview and also i want to write some text on the particular friend

how can i get the Facebook all friend list in my iOS Application tableview and also i want to write some text on the particular friend
I'm using Facebook latest sdk
code :
[FBRequestConnection startWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id<FBGraphUser> result, NSError *error) {
/* handle the result */
NSLog(#"%#", result);
}];
With Facebook SDK 3.0 you can do this:
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);
}
}];
Hope this helps..

Load Facebook Friends into an NSMutableArray

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.

Retrieving Facebook friends list through Facebook iOS SDK

I am trying to retrieve a list of a user's Facebook friends by using the code
[FBRequestConnection startWithGraphPath:#"/me/friends"
parameters:nil HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id 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);
}
}];
But whenever I use that code it just returns Found: 0 friends. I'm putting it inside the Viewcontroller to execute after the user logs in through Facebook. I already requested user_friends permissions and I have a friend who is signed up on the app. If it makes any difference the app is also using Quickblox SDK so if there's any way to do it with that that'd be great!
use this code
FBRequest *friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection *connection,NSDictionary *result ,NSError *error){
NSLog(#"Friend data :%#",result);
NSArray *friends = [result objectForKey:#"data"];
NSLog(#"friends :%#",friends);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#",friend.name,friend.id);
}
}];
Have a look at Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
This is by design and has been discussed dozens of times here.

how to customized fbfrienpickerview controller and show in custom table view

i have got all friends in array
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 %# ", friend.name);
how to display in grouped table view in one section.
How to change (tick) friends selection to some custom button of fbfriendpicker view controller
i had the same issue. So i stored all the friends info into a Array (includes dictionaries) and add that data into a UItableView. So same way i think u can do it.

iOS Facebook SDK - get list of Friends connected with App

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.

Resources