Adding Facebook SDK to iOS Xcode 4 – Simply and Easily

I was surprised at how easy it was to implement a “share story” in my iPhone app. I’ve used ShareKit before, and I actually found the Facebook SDK created by FB to be easier! Note- I’m doing something very simple, publishing a share story. (Yes, that’s the new timline view.)

Anna Billstrom

1. First, download it. In a (non-xcode) directory. Type “git clone git://”

2. In your file system, copy the “src” directory to your app’s project directory and rename something like “fb-sdk”.
3. In Xcode, in your project, add the folder & files “fb-sdk.”

4. Go to, create a new app, and enter in the basic information for a “mobile app” – iOS appID if you’ve already submitted the app, bundleID “com.banane.etc”, and your email. Don’t worry about canvas stuff. Add an icon to make it pretty.

5. Now look at the github’s README page. These are valuable instructions (except for the demo app’s publish example).

6. In your appDelegate.h, 1)import the “FBConnect.h” file, 2) add a property that will be our “facebook” object, and 3) add FB protocols.

#import "FBConnect.h"

@interface yourDelegate : NSObject <UIApplicationDelegate, FBRequestDelegate, FBSessionDelegate, FBDialogDelegate, FBLoginDialogDelegate> {
    Facebook *fb;
// your other props
@property (nonatomic, retain) Facebook *fb;
.. // your other props

7. In your delegate’s implementation file, synthesize and dealloc the “fb” object.

8. Now, start popping in the methods into your appDelegate’s class.
(see end of post)
I have the flow of- user logs in, and if it’s successful, go on to publish the story to the stream. If not, prompt the login sequence. I do this by rigging the methods as:

    NSArray *perms = [[[NSArray alloc] initWithObjects:@"publish_stream",nil] autorelease];
    [self.fb authorize:perms];
(void)fbDidLogin {
    [self publishStream];

The way Facebook works, if you are successfully logged in (your access token has not expired), it will continue with the “fbDidLogin” callback. This works in the web UI too. It’s a way of pushing the validation of login, access-token, and expiration onto Facebook’s API instead of managing it in app.

9. From your view, where you want the Facebook button, create a method such as:

    myDelegate *appDelegate = (myDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate fb_login];

The appDelegate methods (copied largely from the SDK page on Github):

/* fb stuff */
(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    return [self.fb handleOpenURL:url];

    NSArray *perms = [[[NSArray alloc] initWithObjects:@"publish_stream",nil] autorelease];
    [self.fb authorize:perms];
(void)fbDidNotLogin:(BOOL)cancelled {
    NSLog(@"did not login");
(void)fbDidLogin {
    [self publishStream];
(void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"request error: %@",error);
(void)dialogDidComplete:(FBDialog *)dialog {
     NSLog(@"successful publish");
(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"received response");
(void)publishStream {
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    kAppId, @"app_id",
    @"", @"link",
    @"", @"picture",
    @"dialog title", @"name",
    @"little grey text in caption", @"caption",
    @"black colored font paragraph", @"description",
    @"user message – not allowed usually by FB",  @"message",

  [_facebook dialog:@"feed" andParams:params andDelegate:self];}
(void)fbDidLogout {
    NSLog(@"logged out");

/* end fb stuff */