How to parse facebook likes json response? - ios

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.

Related

Obj-C: Check if object exists in NSMutableArray?

I'm trying to check if NSString 'testing' (47) exists inside of my NSMutableArray 'self.checkfriendData'. I'm using the code below, though after logging my if statement it appears as though it's never executed (even though the statement is true - see console data below, uid = 47, and thus hiding my object should fire?) Any idea as to why this isn't working? Help is much appreciated!
ViewController.m
NSMutableDictionary *viewParams3 = [NSMutableDictionary new];
[viewParams3 setValue:#"accepted_friends" forKey:#"view_name"];
[DIOSView viewGet:viewParams3 success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.checkfriendData = (NSMutableArray *)responseObject;
NSString *testing = #"47";
NSArray *friendorNo = self.checkfriendData;
if ([friendorNo containsObject:testing]) // YES
{
self.addFriend.hidden = YES;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
Here's what's inside self.checkfriendData:
2017-05-18 19:36:07.266529-0700 This is the friend data check (
{
body = "My name is Britt";
friendphoto = "/sites/default/files/stored/x.jpg";
"node_title" = "Britt";
uid = 47;
}
)
It appears that your NSArray contains NSDictionarys and you are asking if the array contains an NSString. The answer will always be no as the array doesn't directly contain any NSStrings.
If you want to search for the uid of 47 you will have to iterate over the array and check the uid key of each NSDictionary for the value 47.
The code for this would look something like:
for (NSDictionary *dict in friendorNo) {
if ([dict[#"uid"] isEqualToString:testing]) {
self.addFriend.hidden = YES;
}
}

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"];

AFNetworking 2.0: POST returns success response but server is not updated

I'm using AFNetworking to add Book objects to a server. Here's the setup:
Some dummy Book objects are created for testing:
Book *newBook1 = [[Book alloc] init];
newBook1.title = #"Test Book 1";
newBook1.author = #"Harlan";
Book *newBook2 = [[Book alloc] init];
newBook2.title = #"Test Book 2";
newBook2.author = #"Harlan";
NSArray *books = [NSArray arrayWithObjects:newBook1, newBook2, nil];
[bookCommunicator addBooks:books];
Inside BookCommunicator:
- (void)addBooks:(NSArray *)books
{
for(Book *book in books)
{
[self addBook:book];
}
}
- (void)addBook:(Book *)book
{
NSDictionary *bookProperties = [NSDictionary dictionaryWithObjectsAndKeys:
book.author, #"author",
book.title, #"title",
nil];
[_httpSessionManager POST:#"books" parameters:bookProperties
success:^(NSURLSessionDataTask *task, id responseObject) {
[self.delegate didAddBooks];
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
[self.delegate addingBooksFailedWithError:error];
}
];
}
I get a success response. The responseObject is an array of all of the Book objects already on the server - excluding the new ones. Upon checking the server, these two new book objects are not there. Any ideas as to why?
The issue was not with the code above, but with the API. It required a trailing / on the endpoint (i.e. 'books/') - but it should have accepted both 'books' or 'books/'.

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