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.
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.
Now to post message to user's wall you can use following code.
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.
Thanks for the tut, but how to post message from Qt Symbian app. Please advice.
ReplyDeleteHi,
ReplyDeleteI 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
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.
ReplyDeleteMy email is nan2castillocoello@gmail.com
Many thanks in advance.
Hi, sample code will be same as shown in post, but surely I can share sample project as well.
ReplyDeleteThanks for comment.
Hi, As requested I have posted my sample application here.
ReplyDeletehttps://gitorious.org/kunaltest/kunaltest/trees/master/facebooktest
Hi! How I keep connect in Facebook after close my app?
ReplyDeleteThanks
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.
ReplyDeletehello 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?
ReplyDeleteI guess, you can use description field , for your purpose.
ReplyDeletethanks a lot for the sample...helped me a lot...
ReplyDeletepost in many forum..many people are having problems...
thank you...
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?
ReplyDeleteMy iPhone app is like list of Quote and I want to share it into Facebook without redirect any where.
Thank you
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
Deletethanks lot, its very helpful to me
ReplyDeleteI am doing exactly the same. But the posts don't show up in my facebook wall. Is there anything that I am missing? Thanks
ReplyDeleteThanks 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?
ReplyDeleteThanks in Advance !!!