Just another site

facebook ios login part 3

after part1 and part2 ,

we need to pass the information at singleton to the viewController

so we need to establish our own delegate

now go back to  singleton.h

@protocol SingletonDelegate

-(void) loginReturn:(BOOL)success userInfo:(NSDictionary*)user FailWithError:(NSError*)error;
-(void) logoutReturn;



for singleton.m

1. we need to use facebook method to get the userinfo
2. once the info pass from facebook , then we need pass to the viewcontroller

remember  @synthesize facebook,delegate  at singleton.m;

at the fbDidLogin if success , we use

- (void)fbDidLogin {


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




-(void)request:(FBRequest *)request didLoad:(id)result{

        NSMutableDictionary *userData = [[NSMutableDictionary alloc] init];

        [userData setObject:[result objectForKey:@"email"] forKey:@"email"];
        [userData setObject:[result objectForKey:@"name"] forKey:@"name"];
        [userData setObject:[result objectForKey:@"id"] forKey:@"id"];
        [userData setObject:[result objectForKey:@"first_name"] forKey:@"firstname"];
        [userData setObject:[result objectForKey:@"gender"] forKey:@"gender"];

      [self.delegate loginReturn:YES userInfo:userData FailWithError:nil];
        [userData release];

and at viewController.h , add #import “Singleton.h” and <SingletonDelegate> “;

#import <UIKit/UIKit.h>
#import "Singleton.h"

@interface ViewController : UIViewController<SingletonDelegate>{

    IBOutlet UIButton *loginButton;



in viewController.m viewdidload add

[[Singleton sharedManager] setDelegate:self];

Add the delegate to receive user information

-(void) loginReturn:(BOOL)success userInfo:(NSDictionary *)user FailWithError:(NSError *)error{

    if (success){
        NSLog(@"delegate user %@",user);
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:@"login"];
        [defaults setObject:@"facebook" forKey:@"logintype"];

        NSString *uname = [user objectForKey:@"name"];
        NSString *fbid = [user objectForKey:@"id"];
        NSString *email = [user objectForKey:@"email"];
        NSString *gender = [user objectForKey:@"gender"];

        [defaults setObject:fbid forKey:@"userid"];
        [defaults setObject:uname forKey:@"name"];
        [defaults setObject:email forKey:@"useremail"];
        [defaults setObject:gender forKey:@"gender"];

        [loginButton setTitle:@"Logout" forState:UIControlStateNormal];
    else {
        NSLog(@"login fail");


so try to run it

oh , yes , we can get the information of the user . remember , if the token expires , fail msg will be return or you need to prompt user to login again .

So now we  are going to post Message on the user wall 

first we try to add a button at the viewController page , once the button click , a message will post to user wall

in order to distinguish the two request between get user info and post to wall

so we create two request name

so the new singleton.m look like that

#import <Foundation/Foundation.h>
#import "FBConnect.h"

@protocol SingletonDelegate <NSObject>

-(void) loginReturn:(BOOL)success userInfo:(NSDictionary*)user FailWithError:(NSError*)error;
-(void) logoutReturn;


@interface Singleton : NSObject<FBRequestDelegate,FBSessionDelegate,FBDialogDelegate>{

    Facebook *facebook;
    FBRequest *userRequest;
    FBRequest *postRequest;


@property (retain,nonatomic) Facebook *facebook;


+ (id)sharedManager;

- (void) logoutButtonClicked:(id)sender;

- (void) loginButtonClicked:(id)sender;
-(void) postToWall;


-(void)request:(FBRequest *)request didLoad:(id)result{

    NSLog(@"result %@",result);

    if (request == userRequest){

else if (request == postRequest){



and then add a new method for post wall


    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   kAppId, @"app_id",
                                   @"", @"link",
                                   @"", @"picture",
                                   @"Wordpress", @"name",
                                   @"facebook ios demo", @"caption",
                                   @"singleton ", @"description",

    postRequest = ;


so then create a button at viewController and run the singleton method

so once you press the post button , then get back to the facebook and you can see

the whole source code can be found


Single Post Navigation

4 thoughts on “facebook ios login part 3

  1. you save my day man !!!!!
    thanks allot 🙂

  2. i have a problem, if i close the app and i open it again, return always false, so when i want to post in wall i have this msg :[..] “error Error Domain=facebookErrDomain Code=10000 “The operation couldn’t be completed. (facebookErrDomain error 10000.)[..]” . so how can i fixe that to make user log once.

    • sorry i missed something , add the following at appdelegate or viewdidload or singleton init

      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      if ([defaults objectForKey:@"FBAccessTokenKey"]
      && [defaults objectForKey:@"FBExpirationDateKey"]) {
      facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
      facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

  3. thanks that did it !

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: