How to use the result of FBSDKGraphRequest Outside the Block - ios

I am designing an application which uses Facebook to display the feeds in a table view ..so in order to get the result of the feeds i have called a method FBSDKGraphRequest but I do not know how to retrieve the values of the results from FBSDKGraphRequest outside since they are located in a completion block.I want to display the name and message in the table view....
The code illustration is below:-
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]initWithGraphPath:Path parameters:#{ #"fields": #"feed{from,message,created_time}",} tokenString:accessToken version:FBSDK_TARGET_PLATFORM_VERSION HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(error)
NSLog(#"No Data");
else {
FeedCount = [NSDictionary dictionaryWithDictionary:result];
NSDictionary*feed = [FeedCount objectForKey:#"feed"];
NSArray*array1 = [feed objectForKey:#"data"];
NSArray*array3 = [array1 valueForKey:#"from"];
NSLog(#"Data:%#",array3); ---------------> **Displaying Data**
}
}];
NSLog(#"FeedCount:%#",FeedCount);----------------->**Showing Null**
Any help will be Appreciated

To get the result of dictionary from fbsdk please declare variable as __block NSDictionary *json;
Then assign this inside block like,
json =[FeedCount objectForKey:#"feed"];
Did you try this like,
__block NSDictionary *json;
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]initWithGraphPath:Path parameters:#{ #"fields": #"feed{from,message,created_time}",} tokenString:accessToken version:FBSDK_TARGET_PLATFORM_VERSION HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(error)
NSLog(#"No Data");
else {
FeedCount = [NSDictionary dictionaryWithDictionary:result];
json = [FeedCount objectForKey:#"feed"];
NSArray*array1 = [feed objectForKey:#"data"];
NSArray*array3 = [array1 valueForKey:#"from"];
NSLog(#"Data:%#",array3); ---------------> **Displaying Data**
}
}];
NSLog(#"json:%#",json);

Related

How to use facebook pagination in ios

Hi I have recently started coding for iOS.I am trying to get videos from facebook using the following code.
-(void)facebookGetVideos:(UIViewController*)vc completionHandler:(void (^)(NSMutableArray*))completionBlock{
__block NSMutableArray * itemArray;
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/videos/uploaded"
parameters:#{#"fields":#"source,description,thumbnail,title"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if(error!=nil){
NSLog(#"Error after fb dwnld %#",error);
[MBProgressHUD hideHUDForView:vc.view animated:true];
[self showAlert:#"Error" message:error.localizedDescription viewController:vc];
}else{
NSLog(#"Results %#",result);
itemArray = [[NSMutableArray alloc]init];
// [itemArray addObject:[result valueForKey:#"data"]];
itemArray =[result valueForKey:#"data"];
completionBlock(itemArray);
}
}];
}
The response I am getting from there is as below:-
{
data = (
{
id = 1845903302379295;
source = "https://video.xx.fbcdn.net/v/t43.1792-2/27475816_220074475216744_8742438766032977920_n.mp4?efg=eyJybHIiOjE1MDAsInJsYSI6MTAyNCwidmVuY29kZV90YWciOiJzdmVfaGQifQ%3D%3D&rl=1500&vabr=382&oh=d4c3f6028a979c5919fdd8e444e24179&oe=5A89882A";
},
{
id = 1814936632142629;
source = "https://video.xx.fbcdn.net/v/t42.14539-2/23925286_2650490725061654_2502749796398268416_n.mp4?efg=eyJybHIiOjU3NiwicmxhIjo1MTIsInZlbmNvZGVfdGFnIjoic2QifQ%3D%3D&rl=576&vabr=320&oh=2fde1aea6eff33d8139ccb8fe7a33fd4&oe=5A8980C0";
}
);
paging = {
cursors = {
after = MTgxNDkzNjYzMjE0MjYyOQZDZD;
before = MTg0NTkwMzMwMjM3OTI5NQZDZD;
};
};
}
I am trying to use this after before as parameters as below:-
-(void)afterValues:(NSString*)afterVal{
NSDictionary * parameters =#{#"fields":#"source",#"after": afterVal};
FBSDKGraphRequest *request1 = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/videos/uploaded"
parameters:parameters
HTTPMethod:#"GET"];
[request1 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSLog(#"after videos%#",result);
}];
}
And getting the following as response:-
after videos{
data = (
);
}
Can anyone please point out at my mistake.Any kind of help/suggestion or guidance in this directions would be highly appreciated.Thanks in advance!
Have you requested the manage_pages permission from the respective user through the login dialog? I think you see an empty result because of the missing permission.

schedule post on facebook page using facebooksdk

I have written following code to post on facebook page but getting this error message: “(#200) Unpublished posts must be posted to a page as the page itself.“;
NSString *accesstoken = #"EAACEdEose0cBAAe49xNZBS5MqswXkRptXgLDRuZBZBPZCZCVl5rxW6Bt0jthaHXMACNTiUWePg9XkVTpttDNsLyWYBaEkQaUCm5vbSJQh8zGwkegAcQRjRggAJajunmMyg0jVIKZAZBzMjbZCKWUv1Tie1UzF8gJCpIxACIVgqx7SqKdPZBnIUaElk3meB7SYo6AZD";
NSString *pageId = [dic valueForKey:#"id"];
NSDictionary *dicPara=#{#"message": #"Hello",#"access_token":accesstoken,#"scheduled_publish_time": [NSNumber numberWithInt:timestamp],#"published": #"false"};
NSLog(#"%#",dicPara);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/feed",pageId]
parameters:dicPara
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
if (error)
{
NSLog(#"%#",error);
}
else
{
[arrPostId addObject:[result valueForKey:#"id"]];
}
}];
Now I have searched for this error and found that you have to set accesstoken of the page instead of user's token. So how can I change user's access token to page accesstoken?

How to Retrieve the Results of "FBSDKGraphRequest" to Use Outside?

I am using Facebook SDK for iOS. I can output the results with "NSLog" but I do not know how to retrieve the values of the results from FBSDKGraphRequest outside since they are located in a completion block. I need these values for the later manipulations. I tried to put them in NSArray or NSMutableArray but could not make it.
The code for the illustration is given below:
__block NSMutableArray *results;
__block NSArray *obj;
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"user_groups"]) {
NSLog(#"user_groups permission is granted!");
FBSDKGraphRequest *fgr = [[[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/groups?fields=id"
parameters: nil
HTTPMethod:#"GET"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
//here I can output the results
NSLog(#"User's groups:%#", [result[#"data"] valueForKey:#"id"]);
//trying to pass the values to NSMutableArray
obj = [result[#"data"] valueForKey:#"id"];
results = [NSMutableArray arrayWithArray:obj];
}
}];
}
...
NSLog(#"The size of the array of the results : %lu", [results count]);
Here it seems that NSMutableArray of the results is empty, so, I could not put the data of the results there. How can I retrieve the results from inside of "FBSDKGraphRequest" in order to use them later (externally)? What kind of container would help me?
You need to use [FBSDKTypeUtility arrayValue] to fetch the data.
Something like this:
NSMutableArray *_results;
FBSDKGraphRequest *request =
[[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/groups?fields=id""
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *items = [FBSDKTypeUtility arrayValue:result[#"data"]];
_results = [[NSSet alloc] initWithArray:items];
}
}];
Does this solve the issue for you?
Here is an example of how this is used on the SDK Samples:
https://github.com/facebook/facebook-ios-sdk/blob/652fb84a949ef358ff05afedfb2a7c408bd5c839/FBSDKShareKit/FBSDKShareKit/Internal/FBSDKGameRequestFrictionlessRecipientCache.m#L87

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

get graph Facebook NSDictionary no Data

view.m
#interface ViewController ()
{
NSMutableArray *fbData,*userInfoArray;
NSString *name;
NSString *picture;
NSString *source;
NSMutableArray *myObject;
NSDictionary *dictionary,*userData;
}
code is describeDictionary function
void describeDictionary (NSDictionary *dict)
{
NSArray *keys;
int i, count;
id key, value;
keys = [dict allKeys];
count = [keys count];
for (i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [dict objectForKey: key];
NSLog (#"Key: %# for value: %#", key, value);
}
}
code is Data
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
describeDictionary(userData);
}
}];
code is not Data
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
Data should be identical
help me please i am get Facebook graph api
To retrieve the data used But not available
I do not know what to do then. Find out how to do it.
In your last code block:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
The describeDictionary(userData); line is in the wrong place and that's why it's nil—you haven't completed the graph object request at the point you call describeDictionary. It should be like this:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
describeDictionary(userData);
}
}];
To explain further, look at your original code:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
This is the sequence in which the code is executed:
First, you create the FBRequest object that will be sent to the Facebook API:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
Next, you tell the request object to call the Facebook API and execute the completion handler when it has finished:
[request startWithCompletionHandler:... ];
Next you call describeDictionary, but userData is neither in scope or initialized. The request's completion handler has not yet been executed therefore userData is nil at this point.
describeDictionary(userData);
The completion handler is called asynchronously when the request has finished. It may have finished successfully—in which case user will not be nil—or there may be an error condition. If there is no error, you assign the value of the videos key of the user object to userData. It is only at this point that can you pass userData to describeDictionary:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"];
}
}
You have to call describeDictionary from within the completion handle block.

Resources