How to integrate Facebook with iPhone?

Facebook integration:

 

1-> Go to http://developers.facebook.com/docs/ios/

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

3-> double click on pkg file opens a dialog

fb1

4 → select continue

 

5-> select continue again

fb2

  1. press on install

fb3

  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

fb4

 

      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.

fb5

 

 

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:

 

 

 

Codding:

 

      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 {

[FBAppEventsactivateApp];

[FBAppCallhandleDidBecomeActive];

}

 

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

[FBSession.activeSessionclose];

}

 

 

 

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{

[superviewDidLoad];

FBLoginView *loginview = [[FBLoginViewalloc] init];

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

loginview.delegate = self;

[self.viewaddSubview:loginview];

[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(@”usr_id::%@”,user.id);

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

NSLog(@”usr_Username::%@”,user.username);

NSLog(@”location: %@”,user.location.name);

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];

p.link = [NSURLURLWithString:@”http://developers.facebook.com/ios”];

#ifdef DEBUG

[FBSettingsenableBetaFeatures:FBBetaFeaturesShareDialog];

#endif

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

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

 

 

}

 

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

// see https://developers.facebook.com/docs/reference/api/errors/ 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

[FBSession.activeSessionrequestNewPublishPermissions:@[@”publish_actions”]

defaultAudience:FBSessionDefaultAudienceFriends

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

if (!error) {

action();

} elseif (error.fberrorCategory != FBErrorCategoryUserCancelled){

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

message:@”Unable to get permission to post”

delegate:nil

cancelButtonTitle:@”OK”

otherButtonTitles:nil];

[alertView show];

}

}];

} else {

action();

}

 

}