Someday ago Facebook updated their old SDK for iphone and release a new SDK named IOS Library. This open source iOS library allows you to integrate Facebook into your iOS application include iPhone, iPad and iPod touch. Now its really very easy to implement facebook features in your iOS applications.
Why we should implement Facebook in iOS application:
- According to techcrunch Facebook Mobile Hits 100 Million Users, Growing Faster Than On Desktops. So you can think there are lots of users are now using facebook from iOS base devices.
- According to AppleInsider iPhone sales predicted to top 80 million by 2012
- So people are using iPhone/iPod/iPad more now a days. And the number is growing both for facebook and iOS.
- Why not consider to include facebook users in your iOS application so that your application looks more social.
A typical scenario of facebook features in iOS application:
Suppose you develop a game for iOS. You’ve a online leaderboard system for that game. Now people can compare themselves in the game based on leaderboard. But there might be many fake profile in the leaderboard, because you’re not aware of who’s playing the game. Though facebook has many fake account, but normally a user has real friends in facebook. So you can bring facebook user’s real friends in your application, so that the user feel more proud when he will be in a higher rank among his facebook friends.
So normally you could implement these facebook features in your iOS application:
For a iOS game application:
- Inspire user to connect facebook and brings the data of his friends who already played the game
- When user make higher rank among his friends then show popup to user to publish this news in his wall and friends wall
- If user achieve some good score, show a popup to user to publish the happy news in facebook.
So using facebook’s viral features you can easily market your application to a broad audience.
So you’re inspired to integrate facebook features in your iOS application. I assume you’re using Mac and already installed XCode and you know the basics of how to develop iOS application using XCode and Objective C. Now follow the steps:
- Visit http://github.com/facebook/facebook-ios-sdk/ and download SDK in your laptop
Mahmuds-MacBook-Pro:~ mahmud$ git clone 'http://github.com/erica/iphone-3.0-cookbook-.git'
- In the SDK, now open facebook-ios-sdk/sample/DemoApp/DemoApp.xcodeproj
- Now see the code in Classes/DemoAppViewController.m and create a facebook app and get the appId and place that id in by replacing nil at kAppId variable.
static NSString* kAppId = @"1234444444"; //here 123..... would be your actual app id
- Now build and run the application in your device and test yourself.
- The demo application source code is nicely written. You can easily reuse them for your purpose.
- In the demo you’ll see the following code samples,
- how to authenticate user in facebook
- (void) login { [_facebook authorize:kAppId permissions:_permissions delegate:self]; } - how to call graph api and retrieve information
- (IBAction) getUserInfo: (id)sender { [_facebook requestWithGraphPath:@"me" andDelegate:self]; } - how to call rest api
/** * Example of REST API CAll * * This lets you make a REST API Call to get a user's public information with FQL. */ - (IBAction) getPublicInfo: (id)sender { NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"SELECT uid,name FROM user WHERE uid=4", @"query", nil]; [_facebook requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self]; } - how to show popup to publish wall post
- (IBAction) publishStream: (id)sender { SBJSON *jsonWriter = [[SBJSON new] autorelease]; NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys: @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil]; NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks]; NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys: @"a long run", @"name", @"The Facebook Running app", @"caption", @"it is fun", @"description", @"http://itsti.me/", @"href", nil]; NSString *attachmentStr = [jsonWriter stringWithObject:attachment]; NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: kAppId, @"api_key", @"Share on Facebook", @"user_message_prompt", actionLinksStr, @"action_links", attachmentStr, @"attachment", nil]; [_facebook dialog: @"stream.publish" andParams: params andDelegate:self]; } - how to upload photos in facebook
-(IBAction) uploadPhoto: (id)sender { NSString *path = @"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"; NSURL *url = [NSURL URLWithString:path]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys: img, @"picture", nil]; [_facebook requestWithMethodName: @"photos.upload" andParams: params andHttpMethod: @"POST" andDelegate: self]; [img release]; }
The SDK’s code are very easy to understand and well documented. Also don’t forget to follow the official tutorial of facebook
So you could easily develop iOS base facebook application or you can implement necessary facebook features in your iOS application. So don’t be late, go buddy go

Hope this article will help you to make social iOS application.
- how to authenticate user in facebook










you knowledge is awesome thanks Mahmud !!
@Ccarrasco, thank you. Actually Objective C is a nice language to work with. And I’m working in iphone as a hobby
Hi, great article, I also took a look at the new sdk… actually, I’m having a problem with the post-stream action, can you pls explain how to extend it so my post will have an image next to it, I have a valid image url, and I tried adding it to the ‘attachments’ dictionary as : @”my_pic_url”, @”picture”
but still I get the default image placeholder next to my post….
@Sagi,
I found the problem as you told and fixed it. Just follow the code:
- (IBAction) publishStream: (id)sender { SBJSON *jsonWriter = [[SBJSON new] autorelease]; NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys: @"Always Running",@"text",@"http://thinkdiff.net",@"href", nil], nil]; NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks]; NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys: @"image", @"type", @"http://thinkdiff.net/mahmud_small.jpg", @"src", @"http://thinkdiff.net", @"href", nil]; NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys: @"a long run", @"name", @"The Facebook Running app", @"caption", @"it is fun", @"description", @"http://itsti.me/", @"href", [NSArray arrayWithObjects:imageShare, nil ], @"media", nil]; NSString *attachmentStr = [jsonWriter stringWithObject:attachment]; NSLog(attachmentStr); NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: kAppId, @"api_key", @"Share on Facebook", @"user_message_prompt", actionLinksStr, @"action_links", attachmentStr, @"attachment", nil]; [_facebook dialog: @"stream.publish" andParams: params andDelegate:self]; }The image part should be another NSDictionary object.
NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys: @"image", @"type", @"http://thinkdiff.net/mahmud_small.jpg", @"src", @"http://thinkdiff.net", @"href", nil];And in the attachment NSDictionary object must include imageShare object as array
This is because if you not include this as an array, Json parser avoid [] brackets as a result the publish functionality will not work. Remember the string needs to be a valid JSON string otherwise facebook api will not publish.
thanks, that did the trick.. actually I already tried that before I posted my comment.. but I made a small mistake: I first converted the ‘media’ object into JSON array [...] string, and then included it in the ‘attachments’ dictionary. I didn’t realize I need to put it as a native NSArray with a NSDictionary inside it. thanks!
Thanks for the tip on adding the image to the post. Was trying to track it down in the API docs but it wasn’t spelled out like you have it.
You can use it http://getsharekit.com/install/ very easy to install and it has facebook, twitter, in app email functionality share features.
Great tutorial!
My problem is just that the login view doesn’t get posted on top of my actuall ui. I reallyzed it gets posted just on the main window. As I put my TableView controller on it, the login dialog doesn’t get displayed… (just in the background) how can I solve that?
Thanks
This line is in the FBLoginButton.m file in the DemoApp project but I can’t find it anywhere in the project. Can you explain? Thx.
#import
OK I give up… it’s the import on line 20 of FBLoginButton.m
Hey, I tried the new SDK, but there’s something really bothering me. It seems that every time I load the App, and even push a window after it’s been popped, the session ends and I have to log in again.
How can I resume a session like with the old iPhone SDK?
The structure of the app is like this:
didSelectRowAtIndexPath loads a window with an article. In that window, there’s a button for FB sharing. In order to use it I have to login every time. The Facebook variable is released and initialized like this:
- (void)viewDidLoad { [super viewDidLoad]; _facebook = [[[[Facebook alloc] init] autorelease] retain]; }- (void)dealloc { [_facebook release]; [super dealloc]; }Am I doing something wrong?
Check out the sample app theRunAround included in the SDK. Specifically look at how mainViewController uses the Session object.
definitely make sure rootviewcontroller users the object properly. you can also look at adding the offline param to your permissions request to avoid prompting users to constantly login via your app
Thanks for the help! I took a closer look at the SDK documentation and found out that the Facebook iOS SDK doesn’t handle the session storage. It was foolish of me to overlook that in the first place.
I fixed the error now and everything works like a charm. I sent the app for review so keep your fingers crossed for me
By the way, the app its called TheGossiper and as a thanks for your help I’ll fix you up with some promo codes. Just give me a reply if you’re interested.
Nice to know that you found the solution and informed us. Waiting to see your application and all the best wishes
How would I post a dynamic nsstring say like a highscore? I can easily change the static text but I can’t seem to find a way to integrate an nsstring in place of say the “it is fun” in the description part of the post.
Wow! Great sdk! With the included samples, it took about an hour to integrate FB into my app! Good job everyone!
Have you tried using the latest version in the github? I am having problem with that exact method, publishStream() .. I cannot publish with Dialog error of “An error occurred with [APP_NAME]. Please try again later.”
Did you encounter such problem when running the demo app with publishing?
Everything ok…but i want to change it…that user can write the post in text field and then clicking on publish button (in iphone view.) post this to the user wall..is this possible??
i dont want to show the pop up when we click on publish stream..is this possible??
I tried to run the new sdk, I can login, upload photo,
but I couldn’t publish stream, it gives me Error message ,I followed the steps mentioned in sdk example.
any help plz?
I’m having the same error problem
I implemented it and logged in, but everytime I try to post a status update the blue box comes up with an error message: An error occured. Please try again later.
Can anyone help
Thanks in advance
Andrew
Great Tutorial !!!.
But I am having problem of retaining the session,
I need my app, to stay logged in, even after redirecting to “touch.facebook.com” but the login screen is displayed, what should be passed exactly to redirect URL, (entire format) ?
In the facebook connect demo by facebook, they showed it how to retain session and use. Please check it out.
Ultimate Facebook App
Advance facebook client for iPhone/iPod-Touch
Quickly Upload Videos and Photos to Facebook directly from iPhone. Share photos and videos with friends and family on facebook.
Includes 4 FREE Bonus Apps
1. Phrases/Quick Status – Update your Facebook status with one-click, app contains pre-loaded popular facebook status’s – you can even create your own frequently used status updates.2. Birthday Reminder – Send Virtual Gifts to your friends.3. Watch Facebook videos. 4. View and Create Facebook Events. Geotag your photos on-the-go to automatically capture location data as you shoot. Capture the perfect moments on your iPhone’s camera and instantly share with your friends on Facebook. Make black & white portraits out of color photos. * DoodlePro – a fun app for drawing with fingers and Face Detection. * Birthday reminder – tracks upcoming birthdays of your friends on Facebook. You can even send Virtual Gifts to your friends. * Finding friends Nearby * Check Friends who are online. *
*Download from iTunes:http://itunes.apple.com/us/app/facebook-video-photo-uploader/id349427597?mt=8
Nice, and thanks for sharing this info with us.
Good Luck!
I’m getting crazy. I can’t understand why when I use the demo app inside the sdk and ask to publish a stream(even if I modify permissions), it just publish what i post and not all the data in the sample application (the running facebook test). Could it be related to privacy settings in facebook?
Emm I’m not sure.
Hi…. i just try the code “stream.publis” and it works well repopulate my post, but the problem is if i use the “_facebook dialog: @”stream.publish”…. my app will publish only to my wall and not to my news feed…. can someone help me?… thanks in advance
Instead of using a URL for the image, I would like to use a UIImage object to post the image. Is that possible, if so what changes would be needed to use a UIImage.
I think here’s the code that would need to be changed.
NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
@"image", @"type",
@"http://thinkdiff.net/mahmud_small.jpg", @"src",
@"http://thinkdiff.net", @"href",
nil];
@Dave, I’m not sure if its possible or not. But as far as I know this is not possible.
how to get youtube link from news feed of a user after login with facebook credentials in App.
Hi all. I want to known to upload images on facebook via fbconnect from iphone. Am new to iphone, am struggle with this task can anyone help me. Below is my code, am using FBConncet.
-(void) posttowall
{
NSMutableDictionary *args = [[[NSMutableDictionary alloc] init] autorelease];
[args setObject:fbimages forKey:@"image"]; // ‘fbimages’ is an NSArray of ‘UIImage’ objects,
FBRequest *uploadPhotoRequest = [[[FBRequest alloc] init] autorelease];
uploadPhotoRequest = [FBRequest requestWithDelegate:self];
[uploadPhotoRequest call:@"facebook.photos.upload" params:args];
//[[FBRequest requestWithDelegate:self] call:@”facebook.photos.upload” params:args];
NSLog(@”uploading image is successful”)
}
i want send to image upload request. I received the error when come back response. it description is “errorDomain101″.
What does it means ???
i want to post to my friend some message how to do it…
i am using git hub wala code.
Using http://developers.facebook.com/docs/reference/dialogs/feed/ code from here you can post to your friends wall.
thanks for this , i want to publish facebook checkins and want to post comment on locations , will you please help me. thanks in advance.
Hey ! Thanks for sharing this. Great job.
But i’m still sad that the API doesn’t allow you to share photos taken with your application.
Hope they gonna update it asap.
Once again you wrote amazing post for us.
Thank you for this post.
1. Why the photo only appears once on the wall? I’d like it to appear each time I post?
2. Why the text doesn’t appear with the photo?
Thanks!
Hi,
On Publishstream methods, how to use textView.text for description to publish to the wall instead the user needs to fill the text box.
Please help!
@ iPhone Apps Review: Thanks.
admin: our small team intends to create a simple quiz app for student survey on Facebook. your post is really useful for code rules.
wat is this? same as in the sample provided by facebook developers
how do I get the session uid? I tried to use get the name and email in the request, but I get Mark Zuckerberg for uid=4. Thanks
Found out that you don’t need a session to access user info. should be uid=me() in the SQL statement
Hello,
I am using this sdk but I didn’t got fbDidLogin callback,
I got the error message
“safari cannot open the page because the address is invalid”
I didn’t put any redirect uri for this application.
I am currently using ios SDK 4.2 .
anybody please help me.
Regards,
George
Can we get all comment on the uploaded photo on facebook. Right now I am using graph api:
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/feed" withGetVars:nil];
NSLog(@”getMeFeedButtonPressed: %@”, fb_graph_response.htmlResponse);
But it will not provide the comment on the specific photo. Can you please help me out?
I want to received live call from Facebook on I phone.. Do this possible?
I’m trying to download the sdk and work with your example. I can’t find the sdk downloaded. Where does it wind up and what is it called? Am I looking for it on my mac? I’m sorry but very confused.
Maybe I haven’t downloaded the sdk after all? Where do I download it? Where is the example code to use?
Ok. I think I downloaded it, but there’s an xcode proj called Hackbook, not Demo App. Is that the one for the example?
they updated the sdk. here’s the old one that was used in the example above:
https://github.com/facebook/facebook-ios-sdk/commit/60367ef741ae5063570f793520d9c7559a55fbcc#diff-4