FB Login integration on your iPhone app

One of the simplest way to implement a user login mechanism for your iPhone app is to leverage on facebook’s pre-written fb iOS SDK.  With more than 100 million users using the facebook app on various mobile devices and 68 million+ monthly active iPhone app users at this time of writing, it is almost a sin for an iPhone app developer not to leverage on this and use facebook’s authentication mechanism for his/her app.  I am sure there are exceptions to the rule but for most of the app developers out there, it simply makes sense to use facebook’s login mechanism, especially when facebook’s iOS SDK is well-documented and easy-to-use.

Getting Started

To get started, we can grab the SDK from Github:

calvin$ git clone https://github.com/facebook/facebook-ios-sdk.git

What Now?

In this fb iOS SDK, you will find a demo app in your downloaded facebook-ios-sdk/sample/DemoApp directory. Open that up and you should see something like this in your XCode:

The two files that we have to update in order to build and run this demo app are DemoAppViewController.m and DemoApp-Info.plist.   As indicated in the explanation on https://github.com/facebook/facebook-ios-sdk, DemoApp-Info.plist can be found by traversing down URL types > Item 0 > URL Schemes > Item 0

Creating the FB App and getting the FB App ID

What we need to update in DemoAppViewController.m and DemoApp-Info.plist is the FB App ID.  It only makes sense that facebook knows that your iPhone App contains a valid facebook login mechanism if you have registered an FB App with it.

Luckily for us, getting our FB App ID is a trivial process.  If you haven’t done it before, just hop over to http://www.facebook.com/developers/createapp.php.

Step 1

Step 2

Step 3

And that’s it. You will see your Application ID on the 3rd page in this app creation process on facebook.

And once we have our App ID, all we need to do is to update it in our DemoAppViewController.m file and our DemoApp-Info.plist file.

As shown here:

#import "DemoAppViewController.h"
#import "FBConnect.h"

// Your Facebook APP Id must be set before running this example
// See http://www.facebook.com/developers/createapp.php
// Also, your application must bind to the fb[app_id]:// URL
// scheme (substitue [app_id] for your real Facebook app id).
static NSString* kAppId = @"152465604814562";

and here:

With these in place, we can build and run and we should see our FBLogin button displayed beautifully on our iPhone Simulator, like this:

Happy facebook-ing on your very own iOS App!