With release of iOS5 new concept of SSO came. Now what is this SSO? Why we should use it? “Single Sign On” process is known as SSO. SSO let’s user sign into app only once on a device. That means if user is already sign in to app then they do not need to enter username and password again. Amazing isn’t it.

In case of Facebook SSO the flow is as follows.

  • If user has the Facebook app on device and he/she is already logged in on the Facebook app then user does not need to log in again for your app. It will only ask for user permissions that are necessary for the app.

  • If user has the Facebook app on device but he/she is not logged in to the app then iOS5 opens Facebook app in device for login and user permission and after that calling application will be opened again with access token and expiration date along with other parameters.

  • There can be a case where user does not have Facebook app. In that case iOS5 opens Facebook’s mobile page in safari browser then user logs in from browser and then again calling application will be opened with authorization.

  • If user is using older device, which does not support multi-tasking then the old mechanism of popping up UIWebView, Facebook login and FBSessionDelegate call back which your app should implement.

To start with you need to create app on Facebook account. Go to developers.facebook.com and tap on at top right corner “Apps”. It will ask you to log in to Facebook account. And may be asked for mobile activation also.

Tap on create app and then provide unique name of your app. Then you will get the application ID or APP_ID and application secrete key.

application ID or APP_ID and application secrete key

To start with first download the Facebook SDK here. (https://github.com/facebook/facebook-ios-sdk)

If you are using ARC enabled project then you should be using static library for which there is a build script “build_facebook_ios_sdk_static_lib.s” under script directory of Facebook SDK.

This will create static library under “/facebook-ios-sdk/lib/facebook-ios-sdk”. You may need to copy “facebook-ios-sdk” folder into your app.

If you are not using Automatic Reference Counting then you need to copy “src“ folder into application.

We have created a wrapper class to integrate Facebook SDK easily. You will need to download that wrapper class call “FBUtils” here.

Once you add those files into you application it will look like as below image.

Project Structure

Now there are only 3 steps remaining,

  1. Modify application delegate (AppDelegate)

  2. Implement Custom URL schema.

  3. Initialize, Login method call and delegate.

    Lets’ go through each step one by one.

  1. Modify application delegate (AppDelegate)

// Pre iOS 4.2 support

  • (BOOL)application:(UIApplication *)application

handleOpenURL:(NSURL *)url {

return [[FBUtils sharedFBUtils] handleOpenURL:url];

}

// For iOS 4.2+ support

  • (BOOL)application:(UIApplication *)application

openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication

annotation:(id)annotation {

return [[FBUtils sharedFBUtils] handleOpenURL:url];

}

2. Implement custom url schema with URL as fbAPP_ID

where APPID is the application Identifier which you get when you register the app on Facebook.

URL schema

OR you can right click on .plist file and then select “open as” ? “Source code”. Paste the code below and make sure you add valid APP_ID.

<key>CFBundleURLTypes</key>

<array>

<dict>

<key>CFBundleURLSchemes</key>

<array>

<string>fbAPP_ID</string>

</array>

</dict>

</array>

3. Initialize, Login method call and delegate

To initialize the FBUtils”you need call in following manner

[[FBUtils sharedFBUtils] initializeWithAppID:@””];

You need to provide APP_ID as input. This APP_ID is same as you have provided in Custom URL schema.

To call Login Method First you need to create array with set of permissions like read, publish etc. as per your app requirement. and then call method LoginWithPermission:

NSArray *permision = [NSArray arrayWithObjects:@”read_stream”,@”publish_stream”, nil];

[[FBUtils sharedFBUtils] LoginWithPermisions:permision];

If you want to perform certain operation after login then you need to set delegate as

[FBUtils sharedFBUtils].delegate = self;

and Implement method

-(void)fbDidLogin ;

In this way you can Integrate Facebook with SSO. Now to read and publish operations you will need to know about graph APIs. (http://developers.facebook.com/docs/reference/api/)

You can download the source code from here.