Integrating Facebook Connect to iOS

Tutorial demonstrates the basic usage of the facebook connect sdk for iOS. Here we create an iPhone application that authorizes the user and post a simple link to his timeline using Facebook Connect. Now most of the apps are having social sharing buttons of facebook/twitter.

Step 1

First we need to register our application in Facebook. Login to Facebook and go to

Click on the Create New App button to create a new App. Provide a valid application name. The other 2 fields are optional. Press continue and you will be taken to a captcha verification screen. After verification you will be taken to the App settings screen where you will be provided with the App ID and App Secret.

Write down the App ID and App Secret somewhere or save it as we will need these 2 values in our iPhone App. Please do not share.

Namespace is the identifier which is used to reference your app. For example if its myapp, then the app must be accessible via Fill out the namespace, contact email and categories.

Under “Select how you app integrates with facebook”, click Native iOS App. In iOS Bundle ID field you must enter your iPhone app bundle identifier that you have registered in the iTunes Connect. For example com.mycompanyname.myApp.

Next item to fill is the iPhone app store ID. If your app can be accessed from itunes via the link, then your app ID is 123456000. Do similarly for iPad App. If you do not have and iPad app for the same use 0.

Now we are almost done in setting up the Facebook app. Lets move to code.

Step 2

Lets open xcode and create a new ‘single view application’ with name ‘testapp’. Our application will have only a single button named “Press Me”, which when pressed pushes the link to the user’s timeline.

Goto MainStoryboard.storyboard and place a Round Rect Button in the middle of the screen. Change its title to ‘Press Me’.

We need an IBAction to receive the ‘touch down’ event of the button we created just now. So go ahead and create methods for the same in viewController.h and define it in viewController.m

Goto to MainStoryboard.storyboard and link the button we created with the pressMe action. You can do this by ‘control drag’ button to the ‘File Owner’ and selecting the action pressMe. Do not forget to save.

Step 3. (Integrating Facebook)

Download the facebook iphone sdk from Github -> Click here.

Before referencing the facebook-iphone-sdk you will need to create the facebook sdk static library. To do that run the script facebook-ios-sdk/scripts/ ‘facebook-ios-sdk’ is the sdk folder you downloaded from Github.

After running the script a new file will be created libfacebook_ios_sdk.a at lib/facebook-ios-sdk. Copy the entire folder facebook-ios-sdk inside the lib folder to your project. Now it includes the static library and the headers.

“copy items to destination group’s folder” must be unchecked when you copy.

Step 4. (Implementation)

Import “FBConnect.h” to your viewController. Create an instance of Facebook named facebook. Synthesize it.

Also the delegates FBSessionDelegate and FBRequestDelegate must be implemented.

Initialize the facebook instance with the app ID you saved before in the viewDidLoad Method.

Delegate Functions

  1. -(void)fbDidLogin; //Triggered when user login successful.
  2. -(void)fbDidNotLogin:(BOOL)cancelled //Triggered when user login failed.
  3. -(void)fbDidLogout //Triggered when used did logout.
  4. -(void)fbSessionInvalidated //This will be discussed later.
  5. -(void)request:(FBRequest *)request didLoad:(id)result; //This delegate is called when a request made to the facebook server is successfully completed.
  6. -(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response //Triggered when the request has received some response (may not be complete).
  7. -(void)request:(FBRequest *)request didFailWithError:(NSError *)error //Triggered when the request to facebook server has failed.

Our Strategy

When user presses the ‘Press Me’ button we created before, user will be either logged in already or will not be logged in. Lets first consider the case when the user is not logged in.

CASE 1:(User Not Logged In)

First we need to authorize the user with permissions from the user, for example, to post to his/her timeline.

Now when user presses the “Press me” button a facebook login pops up and asks him permission to both read and write to his timeline. The user will either authorize and login to the application or will cancel the popup. You can handle the situation if the user has cancelled login by adding the delegate function as described below.

Now that user has logged in successfully fbDidLogin Delegate function will be called. Facebook provides us with FB Access Token Key and Facebook Expiration Date Key, which can be used to check whether user is having a valid session. i.e. whether he is logged in or not. We save these details to NSUserDefaults for later use.

We create a NSMutableDictionary to define the parameters that have to be posted to user’s timeline. After this we call facebook’s requestWithGraphPath to invoke the facebook graph API.


The request made above has 2 delegates which denote the success and failure of the request. I have used simple alert popups as coded below to track the success and failure of the Graph request.

How do we find whether the user is logged in?

Remember we stored FB Access Token Key and Facebook Expiration Date Key to NSUserDefaults? Set facebook.accessToken to FB Access Token from NSUserDefaults and facebook.expirationDate to Facebook Expiration Date.

Facebook object has a function isSessionValid to check whether the provided accessToken and expiration date key is valid. So by using the same we can find out whether the user has logged in or not. Please check the code described below.


So the summary of pressMe action is as follows

  1. Set the facebook object with the access token and expiry date key if they are available.
  2. Check for valid session.
  3. If user has a valid session (Already logged in) then directly call the graph api.
  4. If user is not logged in provide him with the login screen.


Admin at

About Geo

Admin at
This entry was posted in iOS and tagged , , , . Bookmark the permalink.

Leave a Reply