Facebook sharing feature in iOS app

Classic/Obsolete(?) way

Install SDK

Drag framework folder into XCode project.

Drag bundle into XCode project.

Drag deprecated header folder into XCode project. check copy in needed.

When picking dependency libraries, set them to optional, to allow app running on iOS5.

Set other link flag to -lsqlite3.0

Those steps are fully documented on facebook developer site: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/3.1/

Note: we are using old fashion Facebook sharing feature in app.  (FBDelegate vs. FBRequest) Because my client doesn’t like posting story on their wall without a preview. This way app will popup a separated window to display link to share, image, title, description, etc. User needs to type something in the textbox with placeholder ‘say something about this…’

Note: again, preset message feature has been disabled by Facebook as of 2012.

In app, we are adding #include “Facebook.h” to implement this.

Note: you can use sharekit library for this, but I want to figure out what’s happening behind the scene.

In the place you want to popup Facebook share options to user, do this to open a Facebook session.

// openSessionWithAllowLoginUI will be called by the actual sharing screen where users can click button to post message on their wall.
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
    NSArray *permission = [[NSArray alloc] initWithObjects:@"user_likes",@"publish_actions", nil];

    return [FBSession openActiveSessionWithPublishPermissions:permission
                                            completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                                [self sessionStateChanged:session state:status error:error];

NSString *const FBSessionStateChangedNotification = @"com.bitrixsoft.Login:FBSessionStateChangedNotifiction";

- (void)sessionStateChanged:(FBSession *)session
                    state:(FBSessionState) state
                    error:(NSError *)error
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                NSLog(@"User session found");
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];

    [[NSNotificationCenter defaultCenter]

    if (error) {
        [[[UIAlertView alloc]
          otherButtonTitles:nil] show];

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    return [FBSession.activeSession handleOpenURL:url];

I put this in appDelegate, so it can be shared. I don’t want to call this in appDelegate, otherwise the redirect to facebook to ask permission/login will annoy users.

Add those in appDelegate.h file

extern NSString *const FBSessionStateChangedNotification;

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

Before calling opensession, sharing window should subscribe statechange notification.

[[NSNotificationCenter defaultCenter]

- (void)sessionStateChanged:(NSNotification *)notification
    if ((FBSession.activeSession.isOpen)) {

        if (nil == self.facebook) {
            self.facebook = [[Facebook alloc]

            self.facebook.accessToken = FBSession.activeSession.accessToken;
            self.facebook.expirationDate = FBSession.activeSession.expirationDate;

            // resume sharing routine here, because first time attempt without login will stop after app back from background, 
            // but this method will get statechange notifcation, so perfect to resume/restart here.
            [self shareViaFacebook];

        self.facebook = nil;

Finally, the actual post action:

- (IBAction)shareOptions:(id)sender

    BNAppDelegate * appDelegate = (BNAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate openSessionWithAllowLoginUI:YES];

    UIActionSheet * options = [[UIActionSheet alloc]
                               otherButtonTitles:@"Twitter", @"Facebook", @"email", nil];

    [options showInView:self.view];
- (void)shareViaFacebook
    NSString * message = @"test msg from my test ios app";

    // no nil allow in any parameter
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Bitrix News iPhone app", @"name",
                                   _post.title, @"caption",
                                   _post.exerpt, @"description",
                                   @"http://www.frankmao.com", @"link",
                                   @"http://ww1.prweb.com/prfiles/2011/02/01/8151699/gI_60506_bxlogo.jpg", @"picture",
                                   message, @"message", //already obsolete, don't bother, facebook will ignore it anyway

    [self.facebook dialog:@"feed" andParams:params andDelegate:self];


Someother gotcha, app identifier must match between app and setting in developers.facebook.com/app, otherwise facebook will complain app is misconfigured when starting the session.

REF: https://developers.facebook.com/docs/reference/dialogs/feed/



4 thoughts on “Facebook sharing feature in iOS app

  1. Thanks for ones marvelous posting! I quite enjoyed reading it,
    you’re a great author.I will be sure to bookmark your blog and definitely will come back at some point. I want to encourage yourself to continue your great work, have a nice holiday weekend!

  2. Hello, i believe that i noticed you visited my website thus i got here
    to return the choose?.I am trying to in finding issues to
    enhance my site!I assume its good enough to make use of
    some of your concepts!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s