Post Data on your friends Facebook wall


In this post we will learn now to populate all your friends name in the table view and then we will be selecting any one friend from the list and we will be posting the data on his/her wall.

Note: I will be upgrading my part 1 tutorial so that I can cut short the detailing part and jump right to the code, so in case if you have missed the action you can visit part 1 of this tutorial.

Revision of tutorial 1: Earlier in tutorial one I have created a view based application which will provide me with the facebook graph api pop up and when I enter my credentials I receive a valid access token with the help of which I am able to post some text on my wall.

 If you are creating an application for any smart phone and want that application to post some messages on facebook then in that case you have to register your application on facebook and get the application ID, with the help of application ID facebook is able to identify that something has been printed or uploaded on your wall with the help of a registered application. It will also display the application name that printed the text of uploaded a file into your facebook wall.

So how do you register your application with facebook ? Well you can read all about it from here

Step 1: Create an empty application in Xcode with two view controller.

- First view controller will call the facebook graph api and will populate the table with the name of your friends and when you will select any one friend on who's wall you want to post data then in that case you will navigate to the second view controller.

- On the second view controller their will be one text view on which you can write the text that you want to post on your friends wall or navigate back to the first view.


Step 2: I have used few code from the tutorial 1 on facebook so i am directly jumping to the response method of the graph API.

When you receive the response in the FBGraphResponse method then earlier to get our information we were using the "me" keyword but now we want to get the list of our friends then in that case you have to use the keyword "me/friends". By using this keyword you will get all the names and facebook ID of your friends in JSON format which you can parse and display the name on the table view.

- (void)FBGraphResponse

{
    @try 
    {
        if (objFBGraph.accessToken
        {
            [self loadAlert];
            SBJSON *jsonparser = [[SBJSON alloc]init];
            
            FbGraphResponse *fb_graph_response = [objFBGraph doGraphGet:@"me/friends" withGetVars:nil];
            
            NSString *resultString = [NSString stringWithString:fb_graph_response.htmlResponse];
            NSDictionary *dict =  [jsonparser objectWithString:resultString];
            
            friendNameArray = [NSMutableArray arrayWithArray:[dict valueForKey:@"data"]];
            [fbfriendsTableView reloadData];
            
            
        }
    }
    @catch (NSException *exception) {
        UIAlertView *objALert = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"Something bad happened due to %@",[exception reason]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [objALert show];
    }
    
    
}




Code Explanation: With the above code you are asking the graph api to get the list of all your friends.

Step 3: When the above code will execute it will populate your table view with the list of all your friends from facebook, so when you select any one person name from the list you should move to the second view controller, so you will write some code in the did select row at index delegate method of the table view in which you will create the object of the second view controller and will push that view in the iPhone window

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    MessageViewController *objMessageVC = [[MessageViewController alloc]init];
    objMessageVC.friendID = [[friendNameArray objectAtIndex:indexPath.row] valueForKey:@"id"];
    objMessageVC.title = [[friendNameArray objectAtIndex:indexPath.row] valueForKey:@"name"];
    
    objMessageVC.graphref = objFBGraph;
    
    [self.navigationController pushViewController:objMessageVC animated:YES];
}




Code Explanation: I have made few properties in the MessageViewController class which will help me to post the data on my friends wall.



Step 4: In the MessageViewController i am having one method that will help me to post data on my friends wall


- (IBAction)postButtonClicked:(id)sender

{
    if ([[messageTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]!=0
    {
        NSMutableDictionary *variables = [[NSMutableDictionary alloc]init];
        [variables setObject:messageTextView.text forKey:@"message"];
        
        [graphref doGraphPost:[NSString stringWithFormat:@"%@/feed",self.friendID] withPostVars:variables];
        
        UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Data Posted on friends wall" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [objAlert show];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    else {
        UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Enter data to post on wall" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [objAlert show];
    }
   
}



Code Explanation: In the first line of code i am checking if the user has entered some data on the text view or not.

- In the second line of code i am creating a mutable dictionary which will post the data on the friends wall.

- graphref is the FBGraph reference from my first view controller with the help of which i can call the graph api method to do post.

- And finally when your post is done you will be navigated back to the main view containing the list of your facebook friends.


Step 5: Alright this is our favorite step, we are all set and done to launch our application on the iPhone simulator. So add the first view controller view to the iPhone window and hit the run button.



                                     























You can download the source code from here.

I hope this post has helped you out in understanding the concept if you have any questions or queries then feel free to contact me via mail or you may enter your queries as comments,

Join us on facebook iPhone by radix Facebook group.

Until then Happy iCoding and have a great Day.

Comments

  1. Hi Radix ,thanks for the tutorial,I have a question:
    Do you know how to edit the redirecturi?
    I don't like when the app load the white page with the "Success message" after you successfully login on Facebook and I would like to remove that page,or maybe hide it

    Thank you

    ReplyDelete
  2. Hi Unknown,

    The success message part is something that is not controlled by me and it's a part of the graph api library that facebook provides to display that the current user is a valid user and is successfully logged in.

    But i will check out if their are any medium to remove that page after success and post the updated code.

    ReplyDelete
  3. objMessageVC.friendID = [[friendNameArray objectAtIndex:indexPath.row] valueForKey:@"id"];
    objMessageVC.title = [[friendNameArray objectAtIndex:indexPath.row] valueForKey:@"name"];

    Getting crash in it......

    ReplyDelete

Post a Comment