Post Data on your friends Facebook wall


Important Message : Facebook has updated the Graph API and as per their Feb 6 2013 norms you cannot post data on your friends wall like the way it's mentioned in the below post, but you have to use the feed dialog to post the data.

Removing ability to post to friends walls via Graph API 
We will remove the ability to post to a user's friends' walls via the Graph API. Specifically, posts against [user_id]/feed where [user_id] is different from the session user, or stream.publish calls where the target_iduser is different from the session user, will fail. If you want to allow people to post to their friends' timelines, invoke the feed dialog. Stories that include friends via user mentions tagging or action tagging will show up on the friend’s timeline (assuming the friend approves the tag).
For more info, see this blog post.

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.


























"The source code of this post has been disabled and will be posted soon"

You may follow our facebook group.

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,

Until then Happy iCoding and have a great Day.

Comments

  1. it is not posting any thing on friends wall

    ReplyDelete
  2. @Harsh Maan : May i know what is the data that you are trying to post, since i have tested the code and it seems to be working fine, it could be nice if a test case is given by you.

    ReplyDelete
  3. i tried this but not posting text on friend wall

    ReplyDelete
  4. @engr salim : Working on the demo i will post the new working demo soon, thanks for the update i would like to apologize to all of you for now i am disabling the source code until i upload a new source.

    ReplyDelete
  5. @All: There has been changes in the graph API due to which we are not able to post the data on friends wall:

    https://developers.facebook.com/roadmap/completed-changes/

    Message From Facebook :

    Removing ability to post to friends walls via Graph API

    We will remove the ability to post to a user's friends' walls via the Graph API. Specifically, posts against [user_id]/feed where [user_id] is different from the session user, or stream.publish calls where the target_id user is different from the session user, will fail. If you want to allow people to post to their friends' timelines, invoke the feed dialog. Stories that include friends via user mentions tagging or action tagging will show up on the friend’s timeline (assuming the friend approves the tag).

    ReplyDelete

Post a Comment