Facebook Inbox with Fql - ios

I need to show the other participant's name and image in a TableView just like Facebook Inbox.
I am using this fql query to get the recipients.
- (void)readInbox {
NSString *query =
[NSString stringWithFormat:#"SELECT recipients,thread_id FROM unified_message where thread_id IN (SELECT thread_id FROM unified_thread WHERE folder = 'inbox')"];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, #"q",
nil];
FBRequest *request = [FBRequest requestWithGraphPath:#"/fql"
parameters:params
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Result: %#", result);
}];
}
and the result is something like this
data = ({
recipients = ({
email = "****#facebook.com";
name = "*****";
"user_id" = 6 * * * ;
}, {
email = "****#facebook.com";
name = "******"; //This is me
"user_id" = * * * ;
});
"thread_id" = "t_mid.1371470061934:11b440f1722f8fc977";
},
Is this correct for what i want to show?How do I get all the other participants (other than me) using fql?

You will receive JSON data using FQL query. Parse it and store it in Mutable Array.
You can use the following query to obtain name and id of a user from his mailbox.
SELECT name, uid FROM user WHERE uid IN (SELECT author_id FROM message WHERE thread_id IN (SELECT thread_id FROM thread WHERE folder_id = 0))
Now the first index would be you. Remove it from the array if you want.

Related

Parse Json from Facebook

I have been trying to integrate with Facebook SDK to login then get the user's data. so I'm using this method after authentication to get the user's details.
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
And the data I'm getting is :
2015-07-08 14:02:40.502 Auth-App[whatever] fetched user:{
"first_name" = Lol;
gender = male;
id = 12345;
"last_name" = Ak;
link = "https://www.facebook.com/...../....";
locale = "en_US";
name = "Lol Ak";
timezone = 4;
"updated_time" = "2015-07-08T10:22:32+0000";
verified = 1;
}
i need to read this array and then display it in text fields.
i tried using results[0] to get the first element in the array but it didn't work.
try this way to access data from dictionary
NSLog("%#",[result objectForKey:#"id"]);
NSLog("%#",[result objectForKey:#"first_name"]);
NSLog("%#",[result objectForKey:#"name"]);
same as read all the fields(key/value) pairs. And set your textFields
Please put two lines your code
NSDictionary *array=[request.responseString JSONValue];
yourtextfield.text=[array valueForKey:#"first_name"];

How to parse facebook likes json response?

I managed to get the user likes using this code:
[FBRequestConnection startWithGraphPath:#"/me/likes"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"User interests data: %#",result);
self.likesTextView.text = [NSString stringWithFormat:#"User interests data: %#",result];
}];
This is the log:
User interests data: {
data = (
{
category = Company;
"created_time" = "2015-04-09T11:31:13+0000";
id = 403422586500762;
name = "Sky3888 iOS & Android - Officially Agent";
},
{
category = "Games/toys";
"created_time" = "2015-04-09T11:30:57+0000";
id = 1575053229378162;
name = i12win;
},
{
category = "Shopping/retail";
"created_time" = "2015-04-07T04:44:09+0000";
id = 273182176129865;
name = Tinkerbelle;
}
);
However how do I get and display the "name" and "category" field in a UILabel?
You can do as below
NSDictionary *responseDict=(NSDictionary *)result;
NSArray *mainArray=[responseDict objectForKey:#"data"];
for (NSDictionary *dict in mainArray) {
nameLabel.text=[dict valueForKey:#"name"];
catLabel.text=[dict valueForKey:#"category"];
}
Hope it helps you..!
Make an NSArray of the result and then loop through the objects in it performing the function objectForKeyon category and name to extract the data you want.
There is a similar question answered here:
NSMutableArray losing all objects
You should be able to use the code from that and copy the results into your UILabel.

Add Key and Values from NSDictionary to NSMutableArray

I have an NSMutableArray with keys UserID and UserName.
I also have an NSDictionary with keys UserID and UserScore.
How do I add the key and values for UserScore from the NSDictionary to the NSMutableArray so that the UserName and UserScore represent the same UserID?
NSMutableArray:
UserID = 123,
UserName = JohnP
NSDictionary:
UserID = 123,
UserScore = 100
Desired output as NSMutableArray:
UserID = 123,
UserName = JohnP,
UserScore = 100
Below is how I retrieve data into the NSMutableArray using Parse and then grab data to a label:
[FBRequestConnection startWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
FacebookFriends = [result objectForKey:#"data"];
}];
NSString *friendname = [[NSString alloc] initWithFormat:#"%#",[[FacebookFriends objectAtIndex:indexPath.row] objectForKey:#"name"]];
I think you really just wanna use an NSMutableDictionary which is made to store value-key pairs.
You can give your mutable dictionary values from your other dictionary simply by pointing to its key-value (objectForKey).
It looks to me like you want to combine two dictionaries into one:
NSDictionary *dict1 = ...;
NSDictionary *dict2 = ...;
NSMutableDictionary *combined = [NSMutableDictionary new];
NSNumber *userid = dict1[#"UserID"];
NSAssert(userid, #"UserID missing from dict1");
NSAssert([userid isEqualToString:dict2[#"UserID"]], #"Mismatched UserIDs");
NSString *username = dict1[#"UserName"];
NSAssert(username, #"Username missing from dict1");
NSNumber *score = dict2[#"UserScore"];
NSAssert(score, #"Score missing from dict2");
combined[#"UserID"] = userid;
combined[#"UserName"] = username;
combined[#"UserScore"] = score;
However I also suspect you have an array of dictionaries. If so, let me know.

Parsing an FQL result

I'm using FQL to get the names of the nearest 5 places like so:
- (void)facebookPlaces {
NSString *query = #"SELECT name FROM place WHERE distance(latitude, longitude, \"39.750655\", \"-104.999127\") < 500 ORDER BY distance(latitude, longitude, \"23.750655\", \"-180.999127\") limit 5";
// Set up the query parameter
NSDictionary *queryParam = #{ #"q": query };
// Make the API request that uses FQL
[FBRequestConnection startWithGraphPath:#"/fql"
parameters:queryParam
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", [error localizedDescription]);
} else {
NSLog(#"Result: %#", result);
// Store the result in an NSData object
NSData *nameData = [result data];
NSLog(#"Test data reads: %#", nameData);
}
}];
The result is neatly returned like this:
Result: {
data = (
{
name = "T|aco";
},
{
name = "ChoLon Bistro";
},
{
name = "Illegal Pete's Lodo";
}, etc...
I stored the data in an NSData object and it reads the exact same way. I would like to parse the result in a way that I can get all of the names in a simple way, and print them to the log. The only way that I could think of was manually parsing the NSData or the result, but I have no idea how to make it only take the stuff in the quotes next to name =. I'm very new to FQL and objective-c, does anyone know how I can do this?
When I researched this, I saw an awesome JSON example that took everything next to name = and stored it in an array. Although it was meant for android and I couldn't figure out how to make it work in iOS. That is ideally what I'm trying to do.
The result is being parsed as a JSON object into an NSDictionary. You can tell because the result object starts with {}. Inside the data key you see an array (denoted by ()).
So to get an array of names you can do this:
NSMutableArray *names = [NSMutableArray array];
for (NSDictionary *item in result[#"data"]) {
[names addObject:item[#"name"]];
}
Is this what you're after?

Facebook iOS SDK pulling friends data issue

I'm trying to implement Facebook iOS SDK 3.1.1 into my app and I'm having issues.
When I try to run
NSString *query =
#"SELECT uid, name, pic_square FROM user WHERE uid IN "
#"(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25)";
// Set up the query parameter
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) {
NSLog(#"Error: %#", [error localizedDescription]);
} else {
NSLog(#"Result: %#", result);
}
}];
My app crashes on NSLog(#"Result: %#", result); If someone could help me out that would be much appreciated. I'm trying to return the user's friends list with each friend's user data. Below is the Debug Navigation view.
Thanks,
Wes
The result you can expect here is a dictionary containing all the friend data you need. You can make sense of it like this:
NSDictionary *resultDict = (NSDictionary *)result;
NSArray *friendsArr = [resultDict objectForKey:#"data"];
NSMutableArray *resultArr = [NSMutableArray array];
for (FBGraphObject *friendObj in friendsArr) {
NSString *name = [friendObj objectForKey:#"name"];
NSString *imageUrl = [friendObj objectForKey:#"pic_square"];
long long uid = [[friendObj objectForKey:#"uid"] longLongValue];
}
I rebuild my project from scratch by adding each controller and library back into an empty project. It appears now everything is working correctly. Same code same spot.

Resources