Sunday, February 13, 2011

Posting message to facebook wall using iPhone SDK

UPDATE: I have posted my sample application code here. Please visit code fore more information.

Recently I was working on iPhone project that required to post message on Facebook wall.

There is already official facebook API for iOS SDK, which we can use for interacting with Facebook.

You can download Facebook API from GitHub repository using following command.
git clone git://github.com/facebook/facebook-ios-sdk.git

To use downloaded facebook API, you need to bring all facebook API source code under your project. To do this you can just drag it's src folder and drop it under your project in XCode.

Before you start coding, you need a valid facebook application id, you can get one from Developer App. And you need to add this App ID in "fbYOUR_APP_ID" format to your project's .plist file like shown in following snapshot.


Now you are ready to use Facebook iPhone SDK. Main interface class to invoke Facebook API is "Facebook". Following is code to use Facebook class to login and autorize application to use user's facebook account.
facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID"];
   NSArray* permissions =  [NSArray arrayWithObjects:@"read_stream", 
    @"publish_stream", nil];
   [facebook authorize:permissions delegate:self];
With read_stream we can access user's basic account information and with publish_stream we can post to user's wall and make comment.

To authorization to complete successfully we need to implement application:handleOpenURL: method from UIApplicationDelegate and forwad call to facebook API. Like shown in below code.
return [facebook handleOpenURL:url]; 
So everything goes fine then you should be able to retrieve user's account information and post to user's wall as well.

Now to post message to user's wall you can use following code.
NSMutableDictionary* params = [[NSMutableDictionary alloc] 
initWithCapacity:1]
 [params setObject:@"Testing" forKey:@"name"];
 [params setObject:@"IMAGE_URL" forKey:@"picture"];
 [params setObject:@"Description" forKey:@"description"];
 [facebook requestWithGraphPath:@"me/feed" 
        andParams:params 
    andHttpMethod:@"POST" 
      andDelegate:self];
Here "me/feed" is used to post message to logged in user's wall, if you want to post to some other user's wall using logged in user's account then use need to use "USER_ID/feed" instead.

15 comments:

  1. Thanks for the tut, but how to post message from Qt Symbian app. Please advice.

    ReplyDelete
  2. Hi,

    I found following links for Qt Facebook API but I personally never tried it, It might help you.

    http://fredyduarte.net/blog/?p=332
    http://code.google.com/p/facebook-cpp-graph-api/w/list

    ReplyDelete
  3. Hi, this functionality is exactly what I need for my app. If you're not too much trouble, could help me with a sample project. I just can not work me, not that I'm doing wrong.

    My email is nan2castillocoello@gmail.com

    Many thanks in advance.

    ReplyDelete
  4. Hi, sample code will be same as shown in post, but surely I can share sample project as well.
    Thanks for comment.

    ReplyDelete
  5. Hi, As requested I have posted my sample application here.

    https://gitorious.org/kunaltest/kunaltest/trees/master/facebooktest

    ReplyDelete
  6. Hi! How I keep connect in Facebook after close my app?

    Thanks

    ReplyDelete
  7. I think you can do that by reusing "access_token" provided by oAuth protocol, store it when exit and use it next time. But you will need to modify api provided by facebook. I dont think that current api support this.

    ReplyDelete
  8. hello i am using your code its perfect but can you tell me how can i post a variable to the user wall for example i want to post a number like 50 to wall but this number is choosen by the user so its dynamic can you tell me how can i do it?

    ReplyDelete
  9. I guess, you can use description field , for your purpose.

    ReplyDelete
  10. thanks a lot for the sample...helped me a lot...

    post in many forum..many people are having problems...

    thank you...

    ReplyDelete
  11. I have used your sample project.. Its helpful me a lot.. but its redirect into safari. you have done code like this or I am missing anything?

    My iPhone app is like list of Quote and I want to share it into Facebook without redirect any where.

    Thank you

    ReplyDelete
    Replies
    1. Yes, it will redirect to safari to get user approval, once user approval is complete, control will come back to your app and do the necessary things

      Delete
  12. thanks lot, its very helpful to me

    ReplyDelete
  13. I am doing exactly the same. But the posts don't show up in my facebook wall. Is there anything that I am missing? Thanks

    ReplyDelete
  14. Thanks For the tutorial.But can you tell me how to post message on own wall and that can be restricted to limited friends or the list we can send,so that only those friends can see that message?
    Thanks in Advance !!!

    ReplyDelete