I have used FQL query in order to retrieve the friends list at a time:
-(void)fetchSaveUserFriendDetails
{
NSString* query = [NSString stringWithFormat:@"SELECT uid,name,birthday_date FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"];
// 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(@"result is %@",result);
NSArray *resultData = [result objectForKey:@"data"];
if ([resultData count] > 0) {
for (NSUInteger i=0; i<[resultData count] ; i++) {
[self.friendsDetailsArray addObject:[resultData objectAtIndex:i]];
NSLog(@"friend details are %@",friendsDetailsArray);
}
}
}
}];
//Save friends to database stuff
..................
}
I get the below output:
I have gone through the official documentation of Facebook Developers FQL queries,but I am unable to find out how to parse the data.
I tried the below way to parse the retrieved data:
NSString *jsonString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSDictionary *jsonValue = [jsonString JSONValue];
NSArray *values = [jsonValue objectForKey:@"friendinfo"];
where result holds all the friends data in JSON format,but I am getting below error:
-JSONValue failed. Error is: Unexpected end of input
UPDATE-INFO
I am trying to parse all the retrieved data(eventually JSON format) and save all the Facebook friends details to my database.All the implementation of retrieval and saving to db part is done in Facebook Sync button action.
Some assistance needed,any help is greatly applauded,thanks :)
Update:
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
result = [result objectAtIndex:0];
}
friends = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *resultData = [result objectForKey:@"data"];
if ([resultData count] > 0) {
for (NSUInteger i=0; i<[resultData count] && i < 25; i++) {
[friends addObject:[resultData objectAtIndex:i]];
}
[friends retain];
[tblInviteFriends reloadData];
} else {
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"No Friends" message:@"You have no friends." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alt show];
[alt release];
}
}
and display that data in cellForRowAtIndexPath
like bellow...
cell.textLabel.text = [[friends objectAtIndex:indexPath.row] objectForKey:@"name"]
define yourDataArray
as a NSMutableArray
and use it in UITableView
for Data display
i hope this is helpful toy you...