Just another site

facebook login for ios -part 2

there are several step

1. go to to setup a new application

let ‘s say “LoginTutorial” , once u click continue

you ‘ll see an app id and app secret, for fb ios , you need app id only

so let’s go back to our xcode

Create a new Xcode project with single View , let ‘s call FacebookLogin

then click the FacebookLogin-info.plist , do the following thing

modify the app plist ?, add URL types, etc ?and finally copy the app id ?but add fb as prefix

such as fb999999999999

then go here

and download the ?”src” and then copy it to the app

so then , we can start writing the program to handle facebook login

and now go to the view first , i want to add a button for login through facebook


#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
    IBOutlet UIButton *loginButton;


now we stop on the viewController , and we need go back to our Singleton

we need to create two new method to handle facebook login and logout

and we need to add the facebook class and handle the delegate

#import <Foundation/Foundation.h>

#import “FBConnect.h”

@interface Singleton : NSObject&lt;FBRequestDelegate,FBSessionDelegate,FBDialogDelegate&gt;{
    Facebook *facebook;
@property (retain,nonatomic) Facebook *facebook;
+ (id)sharedManager;
- (void) logoutButtonClicked:(id)sender;
- (void) loginButtonClicked:(id)sender;

now for the singleton , add various delegate to handle feedback from facebook

#import "Singleton.h"
@implementation Singleton
@synthesize facebook;
#define kAppId @"409043425790610"
static Singleton *sharedMyManager = nil;
+ (id)sharedManager {
    @synchronized(self) {
        if (sharedMyManager == nil)
            sharedMyManager = [[self alloc] init];
    return sharedMyManager;
- (id)init {
    if (self = [super init]) {
        facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
    return self;
#pragma mark
#pragma mark - Facebook Method
- (void) logoutButtonClicked:(id)sender {
- (void) loginButtonClicked:(id)sender {
    if (!) {
        NSLog(@"facebook session");
        NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",@"publish_actions", nil];
        [permissions release];
    else{NSLog(@"session still valid");}
#pragma mark
#pragma mark - Facebook Delegate
- (void) fbDidLogout {
    // Remove saved authorization information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
-(void)request:(FBRequest *)request didFailWithError:(NSError *)error{
    NSLog(@"error %@",error);
-(void)request:(FBRequest *)request didLoad:(id)result{
- (void)fbDidLogin {
    NSLog(@"facebook cancel login");
-(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
    NSLog(@"token extended");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
// Your dealloc method will never be called, as the singleton survives for the duration of your app.
// However, I like to include it so I know what memory I'm using (and incase, one day, I convert away from Singleton).
    // I'm never called!
    [super dealloc];
// Equally, we don't want to generate multiple copies of the singleton.
- (id)copyWithZone:(NSZone *)zone {
    return self;
// Once again - do nothing, as we don't have a retain counter for this object.
- (id)retain {
    return self;
// Replace the retain counter so we can never release this object.
- (NSUInteger)retainCount {
    return NSUIntegerMax;
// This function is empty, as we don't want to let the user release this object.
- (oneway void)release {
//Do nothing, other than return the shared instance - as this is expected from autorelease.
- (id)autorelease {
    return self;

several thing need to take care

1. add #define kAppId @”#############”

2. in init , remember to init the facebook class

facebook = [[Facebook alloc] initWithAppId:kAppId? andDelegate:self];

  • for permission , different permission u need to consider
  • basic – default , get basic information such as friend , name , fb id
  • email – the email of the user
  • publish_actions – the ability to post message on the user’s wall
  • pusblish_stream ?- the ability to post photo to the user album , but this authority the users can grant separately ( for my comment , if ?no need , then don’t ask this right”
  • there is a right for ?offline_access that ur app can get the user information whenever u like, but such authority will be no longer available after may 2012

once we set up the singleton , so we go back to our viewController

#import "ViewController.h"
#import "Singleton.h"
@interface ViewController ()
@implementation ViewController
[[Singleton sharedManager] loginButtonClicked:nil];

so , ok press run , then at least you can see something?

oh yes, then u 'll see the login page of the facebook
1. pls check using this app requires , that's the authority you're asking for
2. if you run in simulator , be u can login your fb account in the safari first before launching the app for test
after press the "LOGIN" , it will redirect to our page
so go to your facebook "Account Settings"using desktop browser ,
go to setting and then apps
you can see that , it means everything going fine

so the next point how we handle the callback
(login information and user information);
from facebook in our apps
facebook using URL callback to pass relevant information to our app
so now we go to the appDelegate to handle this
go to Appdelegate
#import “Singleton.h”
add the following

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [[[Singleton sharedManager]facebook] handleOpenURL: url];

then the url callback we got will pass to singleton to handle
so now we add the relevant delegate in singleton to handle this

- (void)fbDidLogin {


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: forKey:@"FBAccessTokenKey"];
    [defaults setObject: forKey:@"FBExpirationDateKey"];
    [defaults synchronize];


go to search your app plist and u can find that the information such as token and expire date has been copied.

Get user information
you can request user info , post to wall if you have valid token in your device, if the token is invalid , you will get fail message , and then you need to ask for the user to login again !

this part will end , and next part is writing delegate to pass info the viewController we are using , so next part and we will start acquire user info and post to wall


Single Post Navigation

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: