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.
Related
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"];
I am using facebook SDK4.0 in ios i am trying to retrieve logged in user's friendlist by using taggable_friends. I have been able to achieve that.The whole friend list is showing but the problem is with the names. In the returning dictionary some names are showing weird numbers. I've found the reason as well, because the names are of language other than english, most of them are of bengali.Is there any way to get the names in their respective languages?? I am adding the method and snippet of what weird names i am getting.Please help if u know something of this sort!!please!!
-(void) getMineFriends
{
NSDictionary *limitDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"600",#"limit", nil];
[[[FBSDKGraphRequest alloc]initWithGraphPath:#"me/taggable_friends" parameters:limitDictionary HTTPMethod:#"GET"]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"friends = %#",[result objectForKey:#"data"] );
}];
}
Here is an example of the weird name: name = "\U0995\U09be\U099c\U09c0 \U09a4\U09c1\U09b7\U09be\U09b0";
The problem is solved.The problem was that i was accessing the result with object key "data" but when i initialized an array of dictionary with result with object key "data" and then i accessed the name with object key "name" and it came out good.sorry for the inconvenience :P
-(void) getMineFriends
{
NSDictionary *limitDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"600",#"limit", nil];
[[[FBSDKGraphRequest alloc]initWithGraphPath:#"me/taggable_friends" parameters:limitDictionary HTTPMethod:#"GET"]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSArray *friendsarray = [result objectForKey:#"data"];
for (NSDictionary *friend in friendsarray) {
NSLog(#"i have a friend named %#",[friend objectForKey:#"name"]);
}
}];
}
here is the changed code :D
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.
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?
I want to implement "Select All" feature on FBFriendPickerViewController, but I don't really see any ability to select friends from code. Maybe I'm missing something?
I don't know how exactly is the procedure in StackOverflow for similar questions, anyway if it can still be of any help, the solution is posted here.
Quoting:
I have an NSDictionary dict whose key is the facebook user id and the value is the name. Roughly, this is the code:
NSMutableArray *results = [[NSMutableArray alloc] init];
for (id key in dict) {
NSString *name = [dict valueForKey:key];
id<FBGraphUser> user = (id<FBGraphUser>)[FBGraphObject graphObject];
user.id = key;
[user setName:name]; // This is not mandatory
if (user) {
NSLog(#"adding user: %#", user.name);
[results addObject:user];
}
}
// And finally set the selection property
friendPickerController.selection = results;