How to extract Facebook posts information - ios

I'm using this code to read the last post with location of all my facebook friends:
(FacebookFriend is an object I've created mySelf and contains also the friend's id and _facebookFriends contains all my facebook friends)
_postsWithLocationList = [[NSMutableArray alloc] init];
for (FacebookFriend *currentFriend in _facebookFriends) {
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSString *path = [NSString stringWithFormat:#"%#/posts?limit=1&with=location",currentFriend.id];
FBRequest *postsWithLocationRequest = [FBRequest requestWithGraphPath:path parameters:nil HTTPMethod:#"GET"];
[connection addRequest:postsWithLocationRequest completionHandler:^(FBRequestConnection *connection, NSDictionary *result, NSError *error) {
NSDictionary *postData = [result objectForKey:#"data"];
if (postData.count != 0) //some people don't have any post with position
{
// how to extract "place" object?
}
}];
[connection start];
}
and then I would like to store every location in my NSMutable array _postsWithLocationList.
Once I've extracted *postData I printed it's content and it looked like a dictionary (where "place" is a key) exactly as in the Facebook Graph API Explorer.
But when I printed
NSLog(#"%i", postData.count);
I saw that the length of "postData" was always 1, so I think that it's a single object that contains all the instances of the post. I looked at https://developers.facebook.com but I don't understand the type of this object and how to extract it's content.

Related

How to use the result of FBSDKGraphRequest Outside the Block

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);

Facebook Graph API for pages using iOS SDK

I'm trying to retrieve the conversations that the page had with it's users/visitors. I was able to retrieve the conversations for the page using the below call. I do have the right page access token in the 'pgAccessToken' variable.
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:pgAccessToken forKey:#"access_token"];
[params setObject:#"fields" forKey:#"fields"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{page-id}/conversations"
parameters:nil tokenString:pgAccessToken
version:nil HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Output : %#", result);
}];
I was able to receive a list of conversations.
{
"data": [
{
"created_time": "2015-12-12T02:24:21+0000",
"id": "m_mid.1449888061289:b3ec94g1ec0729d776"
},
{
"created_time": "2015-12-12T02:23:08+0000",
"id": "m_mid.1449887988645:94905084ecfdf89812"
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/t_mid.1448167619182:200955bd99614aa136/messages?format=json&access_token={MYACCESSTOKEN}&limit=25&since=1449887061&__paging_token={MYPAGINGTOKEN}&__previous=1",
"next": "https://graph.facebook.com/v2.5/t_mid.1448167619182:2009335d99614aa136/messages?format=json&access_token={MYACCESSTOKEN}&limit=25&until=1448167619&__paging_token={MYPAGINGTOKEN}"
}
}
Now I'm trying to receive messages in the conversation. Which I can get by hitting the node '{conversation-id}/messages' but it just gives me a timestamp and message id as part of the response.
What I want is not just the time stamp and message id, but instead the from, to and message contents. I tried the Graph API Explorer but it would still return the same data when I use the following different nodes.
/{conversation-id}/messages
/{message-id}
Can any one help ?
++++UPDATE++++
FIGURED IT OUT. PASTED BELOW IS THE ANSWER IF SOMEBODY ELSE COMES ACROSS THE SAME ISSUE.
Finally figured this out after a few tries with Graph API Explorer tool on facebook developer page.
The way to get more fields is by supplying the fields I wanted in addition to the default that were being sent to me.
NSString *pgAccessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"PAGE_ACCESS_TOKEN"]; //I have my page accesstoken stored here.
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"message_count,updated_time,participants,snippet" forKey:#"fields"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{page-id}/conversations"
parameters:params tokenString:pgAccessToken
version:nil HTTPMethod:#"GET"];
__weak MessagesTableViewController *weakSelf = self;
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Output : %#", result);
//Process your result here, add to local array, reload table etc.
}];

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

How to get large picture size from Facebook graph API for news feed

I am stuck with getting picture from Facebook Graph API for news feed with small size. This is the url I get https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t1.0-0/10363492_10202184985725870_7374705736674502849_s.jpg. Not sure how will get the picture with size o.jpg
Finally, figured it out. Here is what I did.
NSString *query = [NSString stringWithFormat:#"SELECT pid, object_id, src_big, src_big_width, src_big_height FROM photo WHERE object_id = %#", wallPost[#"object_id"]]; //begins from SELECT........
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, #"q",
nil];
[FBRequestConnection startWithGraphPath:#"/fql" parameters:params HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
//do your stuff
if (!error) {
NSArray *photo = [result objectForKey:#"data"];
NSString *photoString = [[photo valueForKey:#"src_big"]componentsJoinedByString:#""];
//NSLog(#"result %# %#", photoString);
}
}];

iOS Facebook SDK startWithGraphPath:parameters post to just SOME friends programmatically

In Facebook, you can post something and share it with just some friends.
How can this be done programmatically on iOS using Facebook SDK?
My App has an "Emergency Button" that sends: a) User Location on a Map (picture); b) An emergency text (message); and c) The post is only shared with the friends the user has chosen in the config. section (privacy).
- (void)sendMessage {
//Just an Example: Send an Emergency Post with: message, picture, just to SOME friends.
//This action may not need the User to actually press a button.
//Privacy parameter obtained from:
//https://developers.facebook.com/docs/graph-api/reference/v2.0/post/
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:self.emergencyText forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:privacy forKey:#"privacy"];
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error");
NSLog(#"Error descriptcion %#",error.description);
}else{
NSLog(#"Success");
}
}];
}
This code in not running. I get an error from Facebook.
If I comment this line:
// [params setObject:privacy forKey:#"privacy"];
Then it runs fine and I see the post in FB, but it is a public post.
I need to post: message, picture, just to some friends.
Any solution using startWithGraphPath or using any other command is welcome!
You must format the privacy as a JSON string and then assign that json string to the params NSMutableDictionary.
For Example:
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:privacy
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString;
if (! jsonData) {
NSLog(#"Got an error: %#", error2);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"to my friend" forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:jsonString forKey:#"privacy"];

Resources