I am trying to retrieve the list of facebook friends of a logged user in an Iphone app.
this is the code I am using
[_facebook requestWithGraphPath:#"me/friends" andDelegate:self];
the result I get is a NSDictionary instance with only an object inside.
Am I doing something wrong?
You parse your DIctionary Data.
Sample code here,
if ([result isKindOfClass:[NSDictionary class]]) {
NSArray *userInfoArray=[result objectForKey:#"data"];
NSMutableArray *userIdArray=[[NSMutableArray alloc ]init];
NSMutableArray *userNameArray=[[NSMutableArray alloc ]init];
for (int indexVal=0; indexVal<[userInfoArray count]; indexVal++) {
NSDictionary *individualUserInfo=[userInfoArray objectAtIndex:indexVal];
[userIdArray addObject:[individualUserInfo objectForKey:#"id"]];
[userNameArray addObject:[individualUserInfo objectForKey:#"name"]];
}
NSLog(#"Frnd Name Array : %# ",userNameArray);
NSLog(#"Id Array : %# ",userIdArray);
}
Here result is the data you received in request:DidLoad delegate method
Related
I am new to ios development. I am getting certain data from my web services in the form of Dictionary(Which contains a variable and an array of item dictionaries). I am successfully able to store data in userdefaults.
I form a new array named img_urls for a specific key from each item dictionary using for loop.
My problem is: when i access this array using [ img_urls objectAtIndex:0] , i get complete array as output rather than getting value only at 0th index. And giving [img_urls objectAtIndex:1] gives me NSRangeException.
Here is my code:
NSDictionary *restData=[[NSUserDefaults standardUserDefaults]valueForKey:#"Rest data"];
totalResult=[[restData valueForKey:#"totalResult"]intValue];
restaurants=[[NSArray alloc] initWithObjects:[restData valueForKey:#"restaurants"], nil ];
for(NSDictionary *item in restaurants)
{
[img_urls addObject:[item valueForKey:#"profileImage"]];
}
NSLog(#"REST NAMES%#",[img_urls objectAtIndex:0]);
Any help would be appreciated. Thanks in advance!
Change the method like this below:-
[[NSArray alloc] initWithObjects:[restData valueForKey:#"restaurants"], nil ]to
//Modified below
[[NSArray alloc] initWithArray:[restData valueForKey:#"restaurants"]]
First thing, Did you have analyzed an array, Try like this below:-
img_urls=[NSMutableArray array];
NSDictionary *restData=[[NSUserDefaults standardUserDefaults]valueForKey:#"Rest data"];
totalResult=[[restData valueForKey:#"totalResult"]intValue];
restaurants=[[NSArray alloc] initWithObjects:[restData valueForKey:#"restaurants"], nil ];
for(NSDictionary *item in restaurants)
{
[img_urls addObject:[item valueForKey:#"profileImage"]];
}
//Check first array count
if ([img_urls count] > 0)
{
NSLog(#"REST NAMES%#",[img_urls objectAtIndex:0]);
}
You are giving array([restData valueForKey:#"restaurants"]) as a object to restaurants array while initializing so you will get the whole array when getting by using objectAtIndex:0
NSDictionary *restData=[[NSUserDefaults standardUserDefaults]valueForKey:#"Rest data"];
totalResult=[[restData valueForKey:#"totalResult"]intValue];
restaurants=[[NSArray alloc] initWithArray:[restData valueForKey:#"restaurants"]];
for(NSDictionary *item in restaurants)
{
[img_urls addObject:[item valueForKey:#"profileImage"]];
}
NSLog(#"REST NAMES%#",[img_urls objectAtIndex:0]);
I am working on a project where the facebook's friend list have to de displayed. I did all necessary coding to get the reponse , but the reponse is like the following
{"data":[{"name":"Ramprasad Santhanam","id":"586416887"},{"name":"Karthik Bhupathy","id":"596843887"},{"name":"Anyembe Chris","id":"647842280"},{"name":"Giri Prasath","id":"647904394"},{"name":"Sadeeshkumar Sengottaiyan","id":"648524395"},{"name":"Thirunavukkarasu Sadaiyappan","id":"648549825"},{"name":"Jeethendra Kumar","id":"650004234"},{"name":"Chandra Sekhar","id":"652259595"}
Can anyone please tell me how to save name and id in two different arrays.
Any help will be appreciated.
you can see below how html response parse . there i am getting facebook friends.
- (void)fbGraphCallback:(id)sender
{
if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) )
{
//restart the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
}
else
{
NSLog(#"------------>CONGRATULATIONS<------------, You're logged into Facebook... Your oAuth token is: %#", fbGraph.accessToken);
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:#"me/friends" withGetVars:nil];// me/feed
//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary * facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
//init array
NSMutableArray * feed = (NSMutableArray *) [facebook_response objectForKey:#"data"];
// NSMutableArray *recentFriends = [[NSMutableArray alloc] init];
arr=[[NSMutableArray alloc]init];
//adding values to array
for (NSDictionary *d in feed)
{
[arr addObject:d];
}
//NSLog(#"array is %# ",arr);
[fbSpinner stopAnimating];
[fbSpinner removeFromSuperview];
[myTableView reloadData];
}
}
This is json response you are getting. So you need a JSON parser to convert this string into Objective-C objects. In iOS App, you can use a library like the json-framework. This library will allow you to easily parse JSON and generate json from dictionaries / arrays (that's really all JSON is composed of).
From SBJson docs: After JSON parsing you will get this conversion
JSON is mapped to Objective-C types in the following way:
null -> NSNull
string -> NSString
array -> NSMutableArray
object -> NSMutableDictionary
true -> NSNumber's -numberWithBool:YES
false -> NSNumber's -numberWithBool:NO
integer up to 19 digits -> NSNumber's -numberWithLongLong:
all other numbers -> NSDecimalNumber
That looks like JSON, not HTML. (You probably already knew this, since you tagged the question with json I see.)
I'm not really sure why others are recommending third-party libraries to do this, unless you need to support rather old OS releases. Just use Apple's built-in NSJSONSerialization
class.
This is not HTML. This is JSON. You'll need a JSON parser for this.
A JSON parser would typically make an NSDictionary or NSArray out of the string. With my implementation, you'd do something like this:
NSMutableArray *names = [NSMutableArray array];
NSMutableArray *ids = [NSMutableArray array];
NSDictionary *root = [responseString parseJson];
NSArray *data = [root objectForKey:#"data"];
for (NSDictionary *pair in data)
{
[names addObject:[pair objectForKey:#"name"]];
[ids addObject:[pair objectForKey/#"id"]];
}
Recent versions of iOS contain a new Foundation class, NSJSONSerialization, that will handle any JSON parsing and serialization for you.
I am working on application where I want to access user friends list by login into facebook application. I am using xcode 4.2 ios 5 framework. I have gone through many tutorial but didn't find proper solution. please help me out.
- (void)getFriendsResponse {
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:#"me/friends" withGetVars:nil];// me/feed
//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary * facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
//init array
NSMutableArray * feed = (NSMutableArray *) [facebook_response objectForKey:#"data"];
NSMutableArray *recentFriends = [[NSMutableArray alloc] init];
//adding values to array
for (NSDictionary *d in feed) {
NSLog(#"see Dicitonary :%#",d );
facebook = [[Facebook alloc]initWithFacebookDictionary:d ];
[recentFriends addObject:facebook];
NSLog(#"Postsss :->>>%#",[facebook sender]);
[facebook release];
}
friends = recentFriends;
[self.tableView reloadData];
}
use facebook graph api. To get a list of their friend lists, you need read_friendlists extended permission. Graph Api reference and permissions
currently i m working on iphone application(facebook connect),
Is it possible to post the messages to MULTIPLE friends walls? Currently i am able to send message on single friend wall using "PUBLISH STREAM".
SO Using publish stream is it possible to send message on multiple friend's wall at a time ??
There is no such functionality I guess. But you can do it.
Check the example:
These are Fb delegate methods add to the class.
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
};
Getting friends UIDs: after logged in get the friends details and store the friends UIds in an array 'uids'
- (void)request:(FBRequest *)request didLoad:(id)result {
if([result isKindOfClass:[NSDictionary class]]) {
NSLog(#"dictionary");
result=[result objectForKey:#"data"];
if ([result isKindOfClass:[NSArray class]]) {
for(int i=0;i<[result count];i++){
NSDictionary *result2=[result objectAtIndex:i];
NSString *result1=[result2 objectForKey:#"id"];
NSLog(#"uid:%#",result1);
[uids addObject:result1];
}
}
}
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
NSString *dataresponse=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data is :%#",dataresponse);
}
Posting To All FB Friends:
Itterate the post till [uids count];
- (void)conncetToFriends:(id)sender {
static int ij=0;
NSMutableDictionary* params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:appId, #"api_key", #"Happy Holi", #"message", #"http://www.holifestival.org/holi-festival.html", #"link", #"http://www.onthegotours.com/blog/wp-content/uploads/2010/08/Holi-Festival.png", #"picture", #"Wanna Kno abt HOLI.. Check this...", #"name", #"Wish u 'n' Ur Family, a Colourful day...", #"description", nil];
NSLog(#"uid count:%i",[uids count]);
for(int i=0;i<[uids count];i++) {
NSString *path=[[NSString alloc]initWithFormat:#"%#/feed",[uids objectAtIndex:i]];
NSLog(#"i value:%i",ij);
//[facebook dialog:#"me/feed" andParams:params1 andDelegate:self];
[facebook requestWithGraphPath:path andParams:params1 andHttpMethod:#"POST" andDelegate:self];
ij++;
}
}
I'm struggling with the graph api for a few days now and I can't seem to get any further.
In my appdelegate i've placed the following code within the didFinishLaunching
facebook = [[Facebook alloc] initWithAppId:#"cut-app-id-out"];
NSArray* permissions = [[NSArray arrayWithObjects:
#"email", #"user_location", #"user_events", #"user_checkins", #"read_stream", nil] retain];
[facebook authorize:permissions delegate:self];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"test message from stackoverflow", #"message",
nil];
[facebook requestWithGraphPath:#"/me/feed" andParams:params andDelegate:self];
as you can see i'm trying to get the users feed out. I use the following code to place the data into a proper dictionary.
- (void)request:(FBRequest*)request didLoad:(id)result
{
if ([result isKindOfClass:[NSDictionary class]]) {
NSLog(#"count : %d ", [result count]);
NSArray* resultArray = [result allObjects];
NSLog(#"count : %d ", [result count]);
result = [resultArray objectAtIndex:0];
NSLog(#"count : %d ", [result count]);
result = [result objectAtIndex:0];
}
}
But the didFailWithError: function gives me the following error:
The operation couldn’t be completed. (facebookErrDomain error 10000.)
I've place the FBRequestDelegate in my interface file as following:
#interface TabbedCalculationAppDelegate : NSObject
<FBRequestDelegate>{
Is there something what i'm missing?
There's a lot more needed to handle Facebook responses, especially after authorization. You're not going to be able to make calls against the Facebook API immediately after the authorize method. Please see their sample code which shows how to respond to the various events which you will need to do, most importantly the fbDidLogin and fbDidNotLogin ones. Only once you have a successful login should you access the Graph API.
In addition, it looks like you're trying to post a message with the Graph API. Unfortunately that's not the way to do it and a call like [facebook requestWithGraphPath:#"me/feed" andDelegate:self]; is meant for read-only use. Again, look at the above link for an example of how to use the publish_stream permission (their code has a publishStream example method).