Integrate Facebook in iOS


 

1. Download Facebook sdk from  https://developers.facebook.com/ios/

2. add it in application with FacebookSDKResources.bundle and FBUserSettingsViewResources.bundle  from Facebook sdk resources.

3. Add following Framework

*social.framework

* Account.framework

* AdSupport.Framework

*AddressBook.Framework

*corelocation.Framework

*coreGraphics.Framework

* libsqlite3.0.dylib

4.in AppDelegate .h

#import <UIKit/UIKit.h>

#import <FacebookSDK/FacebookSDK.h>

#define BAAlert(TITLE,MSG) [[[[UIAlertView alloc] initWithTitle:(TITLE) \

message:(MSG) \

delegate:nil \

cancelButtonTitle:@”OK” \

otherButtonTitles:nil] autorelease] show]

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

UINavigationController *navController;

}

extern NSString *const FBSessionStateChangedNotification;

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@property (strong, nonatomic) UINavigationController *navController;

– (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

– (void) closeSession;

@end

5. in AppDelegate.m

#import <FacebookSDK/FBSessionTokenCachingStrategy.h>

write  

NSString *const FBSessionStateChangedNotification =

@”com.laitkor.PlayWithFacebook:FBSessionStateChangedNotification”; after at start of page after @implementation.

add following Method

– (BOOL)application:(UIApplication *)application

openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication

annotation:(id)annotation {

// attempt to extract a token from the url

return [FBSession.activeSession handleOpenURL:url];

}

add following Method

– (void)sessionStateChanged:(FBSession *)session

state:(FBSessionState) state

error:(NSError *)error

{

switch (state) {

case FBSessionStateOpen:

if (!error) {

// We have a valid session

NSLog(@”User session found”);

}

break;

case FBSessionStateClosed:

case FBSessionStateClosedLoginFailed:

[FBSession.activeSession closeAndClearTokenInformation];

break;

default:

break;

}

[[NSNotificationCenter defaultCenter]

postNotificationName:FBSessionStateChangedNotification

object:session];

if (error) {

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@”Error”

message:error.localizedDescription

delegate:nil

cancelButtonTitle:@”OK”

otherButtonTitles:nil];

[alertView show];

}

}

– (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

NSArray *permissions = [[NSArray alloc] initWithObjects:

@”user_location”,

@”user_birthday”,

@”user_likes”,

@”friends_likes”,

@”friends_birthday”,

@”read_mailbox”,

nil];

return [FBSession openActiveSessionWithReadPermissions:permissions

allowLoginUI:allowLoginUI

completionHandler:^(FBSession *session,

FBSessionState state,

NSError *error) {

[self sessionStateChanged:session

state:state

error:error];

}];

}

– (void) closeSession {

[FBSession.activeSession closeAndClearTokenInformation];

}

*write

[FBSession.activeSession handleDidBecomeActive];

inside ApplicationDidBecomeActive method

* write 

[FBSession.activeSession close];

inside ApplicationWillTerminate method.

6.in viewController.h

#import <UIKit/UIKit.h>

#import <FacebookSDK/FacebookSDK.h>

@interface ViewController : UIViewController<FBLoginViewDelegate>{

IBOutlet UIButton *fbButton;

IBOutlet NSDictionary *frndDetail;

IBOutlet NSMutableArray *myData;

}

@property(nonatomic, retain)UIButton *fbButton;

– (IBAction)authButtonAction;

-(void)fetchFriendsList;

7. inside viewController.m

*import 

#import “AppDelegate.h”

@synthesize fbButton;

*write in viewDidLoad

[[NSNotificationCenter defaultCenter]

addObserver:self

selector:@selector(sessionStateChanged:)

name:FBSessionStateChangedNotification

object:nil];

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

[appDelegate openSessionWithAllowLoginUI:NO];

*add Following Method

– (IBAction)authButtonAction {

AppDelegate *appDelegate =

[[UIApplication sharedApplication] delegate];

// If the person is authenticated, log out when the button is clicked.

// If the person is not authenticated, log in when the button is clicked.

if (FBSession.activeSession.isOpen) {

//[appDelegate closeSession];

} else {

// The person has initiated a login, so call the openSession method

// and show the login UX if necessary.

[appDelegate openSessionWithAllowLoginUI:YES];

}

}

add a button and connect this to above authButtonAction function .

– (void)sessionStateChanged:(NSNotification*)notification {

if (FBSession.activeSession.isOpen) {

FBRequest* friendsRequest = [FBRequest requestForMyFriends];

[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,

NSDictionary* result,

NSError *error) {

NSArray* friends = [result objectForKey:@”data”];

NSLog(@”Found: %@ friends”, result);

frndDetail=[NSMutableDictionary dictionary];

frndDetail=result;

UIAlertView *av1=[[UIAlertView alloc] initWithTitle:@”data” message:[NSString stringWithFormat:@”%@”,frndDetail] delegate:self cancelButtonTitle:@”ok” otherButtonTitles:@”no”, nil];

[av1 show];

[self moveToNextWindow];

}];

[FBRequestConnection

startForMeWithCompletionHandler:^(FBRequestConnection *connection,

id<FBGraphUser> user,

NSError *error) {

/*  UIAlertView *av=[[UIAlertView alloc] initWithTitle:@”data” message:[NSString stringWithFormat:@”%@”,user] delegate:self cancelButtonTitle:@”ok” otherButtonTitles:@”no”, nil];

[av show];*/

if (!error) {

NSString *userInfo = @””;

NSLog(@”data is      %@”,user);

// Example: typed access (name)

// – no special permissions required

userInfo = [userInfo

stringByAppendingString:

[NSString stringWithFormat:@”Name: %@\n\n”,

user.name]];

// Example: typed access, (birthday)

// – requires user_birthday permission

userInfo = [userInfo

stringByAppendingString:

[NSString stringWithFormat:@”Birthday: %@\n\n”,

user.birthday]];

// Example: partially typed access, to location field,

// name key (location)

// – requires user_location permission

userInfo = [userInfo

stringByAppendingString:

[NSString stringWithFormat:@”Location: %@\n\n”,

[user.location objectForKey:@”name”]]];

// Example: access via key (locale)

// – no special permissions required

userInfo = [userInfo

stringByAppendingString:

[NSString stringWithFormat:@”Locale: %@\n\n”,

[user objectForKey:@”locale”]]];

// Example: access via key for array (languages)

// – requires user_likes permission

if ([user objectForKey:@”languages”]) {

NSArray *languages = [user objectForKey:@”languages”];

NSMutableArray *languageNames = [[NSMutableArray alloc] init];

for (int i = 0; i < [languages count]; i++) {

[languageNames addObject:[[languages

objectAtIndex:i]

objectForKey:@”name”]];

}

userInfo = [userInfo

stringByAppendingString:

[NSString stringWithFormat:@”Languages: %@\n\n”,

languageNames]];

}

// Display the user info

//self.userInfoTextView.text = userInfo;

myData=[NSString stringWithFormat:@”%@”,user];

NSLog(@”data is      %@”,myData);

}

//[self moveToNextWindow];

}];

} else {

[fbButton setTitle:@”Login” forState:UIControlStateNormal];

}

}

8. at the last open plist file

add new field named as    FacebookAppID

and put your Facebook id like 233936543368dfsde33  if in the value column.

add new field URL types—>item 0–> URLschemes–> item 0 and enter your Facebook id with prefix fb  like  fb233936543368dfsde33.

best of luck.

Advertisements

One thought on “Integrate Facebook in iOS

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: