How to integrate Facebook with iPhone?

Facebook integration:


1-> Go to

2-> Download latest Facebook SDK for iOS.pkg file

3-> double click on pkg file opens a dialog


4 → select continue


5-> select continue again


  1. press on install


  1. NOTE: By default, the Facebook SDK for iOS is installed in ~/Documents/FacebookSDK



Create a App id in Facebook account:


      1. Login with id password in developer account in facebook
      2. select Apps tab in it
      3. select +Create New App



      1. enter details and press continue
      2. In the next dialog select Native iOS App and fill the data with bundle id and app id and save details.

Note: Make sure bundle id in iphone project and facebook both are same.




Create a project



1. The Facebook SDK for iOS is a framework that you add to your Xcode project. To add the SDK, open ~/Documents/FacebookSDK and drag the FacebookSDK.framework folder into the Frameworks section of your Project Navigator.


2. Configure the .plist


→ Finally, you need to place the Facebook app ID in two places in your application’s main .plist file, and add a reference to your app’s Display Name.


→ Create a key called FacebookAppID with a string value, and add the app ID there. Next, create a key called FacebookDisplayName with a string value, and add the Display Name you configured in the App Dashboard.


→ Create an array key called URL types with a single array sub-item called URL Schemes. Give this a single item with your app ID prefixed with fb. This is used to ensure the application will receive the callback URL of the web-based OAuth flow.

→ The finished .plist should look something like this:






      1. Design user interface design with Login button
      2. #import <FacebookSDK/FacebookSDK.h> in AppDelegate
      3. Replace with code in AppDelegate.m file


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

// attempt to extract a token from the url

return [FBAppCallhandleOpenURL:url sourceApplication:sourceApplication

fallbackHandler:^(FBAppCall *call) {

NSLog(@”In fallback handler”);





– (void)applicationDidBecomeActive:(UIApplication *)application {





– (void)applicationWillTerminate:(UIApplication *)application {






In ViewController.h file

1. #import <FacebookSDK/FacebookSDK.h>

      1. add delegate FBLoginViewDelegate
      2. @property (strong, nonatomic) id<FBGraphUser> loggedInUser;

@property (strong, nonatomic) FBProfilePictureView *profilePic;

            • (void)showAlert:(NSString *)message result:(id)result error:(NSError *)error;


in ViewController.m file

– (void)viewDidLoad{


FBLoginView *loginview = [[FBLoginViewalloc] init];

loginview.frame = CGRectMake(50, 200, 150, 40);

loginview.delegate = self;


[loginview sizeToFit];

// Do any additional setup after loading the view from its nib.



and put these methods in .m file

#pragma mark Facebook Login Delegate


– (void)loginViewFetchedUserInfo:(FBLoginView *)loginView

user:(id<FBGraphUser>)user {

// here we use helper properties of FBGraphUser to dot-through to first_name and

// id properties of the json response from the server; alternatively we could use

// NSDictionary methods such as objectForKey to get values from the my json object


// self.labelFirstName.text = [NSString stringWithFormat:@”Hello %@!”, user.first_name];

NSLog(@”UserName: %@”,user.first_name);




NSLog(@”location: %@”,;

self.loggedInUser = user;



– (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {

// test to see if we can use the share dialog built into the Facebook application

FBShareDialogParams *p = [[FBShareDialogParamsalloc] init]; = [NSURLURLWithString:@””];

#ifdef DEBUG



// BOOL canShareFB = [FBDialogs canPresentShareDialogWithParams:p];

// BOOL canShareiOS6 = [FBDialogs canPresentOSIntegratedShareDialogWithSession:nil];





– (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {

// see for general guidance on error handling for Facebook API

// our policy here is to let the login view handle errors, but to log the results

NSLog(@”FBLoginView encountered an error=%@”, error);



#pragma mark –


// Convenience method to perform some action that requires the “publish_actions” permissions.

– (void) performPublishAction:(void (^)(void)) action {

// we defer request for permission to post to the moment of post, then we check for the permission

if ([FBSession.activeSession.permissionsindexOfObject:@”publish_actions”] == NSNotFound) {

// if we don’t already have the permission, then we request it now



completionHandler:^(FBSession *session, NSError *error) {

if (!error) {


} elseif (error.fberrorCategory != FBErrorCategoryUserCancelled){

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@”Permission denied”

message:@”Unable to get permission to post”




[alertView show];



} else {