Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:56:39+00:00 2026-06-01T22:56:39+00:00

I have had the Facebook SDK working in the APPDelegate as instructed by the

  • 0

I have had the Facebook SDK working in the APPDelegate as instructed by the Facebook tutorial, however I am trying to put in into a singleton method. Every tutorial i’ve found seems to be for an older version of the SDK, but I have managed success with this one http://indiedevstories.com/2011/08/30/a-facebook-reusable-class/

I’m having 2 problems, the first is posted here this is the second:

I want a button to post to Facebook, but if the user isn’t logged in then they need to login first then post (without having to press a separate login button first).
I can log in fine, and I can post fine, however I can’t do both together.
If not logged in, the post code shows the login screen, but doesn’t goto the post screen after logging in. you have to press post again.
If you try to login but are already logged in, then nothing happens.
So on the button I use code to login then post, as the login is just skipped if already logged in.
The problem i’m having, is the post code is being run instantly after the login code, so before the user has had a chance to login. This results in 2 popups being opened (1 login and 1 post which displays login as not logged in yet).

How can I get my code to wait for the user to login before moving onto the next line in the code to post?

FacebookHelper.h

@interface FacebookHelper : NSObject <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate, FBLoginDialogDelegate> {
    Facebook *_facebook;
    NSArray *_permissions;
}

@property (nonatomic,strong) Facebook *facebook;

+(FacebookHelper *) sharedInstance;
+(void)fbDidLogin;

#pragma mark - Public Methods
-(BOOL) isFBSessionValid;
-(void) login;
-(void) logout;
-(void) postToWallWithDialogNewHighscore:(int)highscore;

@end

FacebookHelper.m

@implementation FacebookHelper
@synthesize facebook;

#pragma mark -
#pragma mark Singleton Variables
static FacebookHelper *singletonDelegate = nil;

#pragma mark -
#pragma mark Singleton Methods

+(FacebookHelper *)sharedInstance
{
    @synchronized(self) {
        if (singletonDelegate == nil) {
            singletonDelegate = [[self alloc] init]; // Assignment not done here
        }
    }
    return singletonDelegate;
}

-(id)init
{
    self = [super init];
    if (self) {
        _facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
        // Restore previous session
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
        _facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        _facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }
        //
    }
    return self;
}

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (singletonDelegate == nil) {
            singletonDelegate = [super allocWithZone:zone];
            // assignment and return on first allocation
            return singletonDelegate;
        }
    }
    //on subsequent allocation attemps, return nil
    return nil;
}

-(id)copyWithZone:(NSZone *)zone
{
    return self;
}

#pragma mark - Facebook Delegate Methods

-(void)fbDidLogin
{
    NSLog(@"fbDidLogin");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

-(void)fbDidLogout
{
    NSLog(@"fbDidLogout");
    // Remove saved authorisation information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }
}

#pragma mark - Public Methods

-(NSMutableDictionary *) buildPostParamsWithHighScore:(int)highscore
{
    NSString *customMessage = [NSString stringWithFormat:kCustomMessage, highscore, kAppName];
    NSString *postName = kAppName;
    NSString *serverLink = [NSString stringWithFormat:kServerLink];
    NSString *imageSrc = kImageScr;

    //Final params build
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   //@"message", @"message",
                                   imageSrc, @"picture",
                                   serverLink, @"link",
                                   postName, @"name",
                                   @" ", @"caption",
                                   customMessage, @"description",
                                   nil];
    return params;
}

-(BOOL) isFBSessionValid
{
    // Check if there is a valid session
    //_facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
    _facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"FBAccessTokenKey"];
    _facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"FBExpirationDateKey"];
    NSLog(@"accessToken=%@ expirationDaate=%@",_facebook.accessToken,_facebook.expirationDate);
    if (![_facebook isSessionValid]) {
        NSLog(@"FacebookHelper isFBSessionValid = NO");
        return NO;
    } else {
        NSLog(@"FacebookHelper isFBSessionValid = YES");
        return YES;
    }

    return NO;
}

-(void) login
{
    NSLog(@"FacebookHelper login");
    _permissions = [NSArray arrayWithObjects:@"publish_stream", nil]; //@"read_stream", @"offline_access"
    [_facebook authorize:_permissions];
}

-(void) logout
{
    [_facebook logout];
}

-(void) postToWallWithDialogNewHighscore:(int)highscore
{
    NSMutableDictionary *params = [self buildPostParamsWithHighScore:highscore];

    NSLog(@"Post Feed");
    [_facebook dialog:@"feed" andParams:params andDelegate:self];
}

@end

Button Action:

- (IBAction)facebookTest:(id)sender {
    [[FacebookHelper sharedInstance] login];
    [[FacebookHelper sharedInstance] postToWallWithDialogNewHighscore:123];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:56:40+00:00Added an answer on June 1, 2026 at 10:56 pm

    I have updated the singleton class to post scores to Facebook Wall. Check out the new version: http://indiedevstories.com/2012/04/11/facebookscorer-post-highscores-to-users-facebook-wall/

    This new version handles correctly the internal state when authorization and login is required.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had the Facebook SDK working in the APPDelegate as instructed by the
I am trying to integrate Facebook into my app and have already implemented MGTwitterEngine
Since I have just started working with Facebook's api I had a few questions
I'm trying to start php development for facebook applications. I have already had some
I have had been on a roller coaster trying to get ImageMagick to work
I try to follow the facebook-android tutorial from here: https://developers.facebook.com/docs/mobile/android/build/#sample I am working in
Users have been able to log into my website using their Facebook account, but
I'm trying to integrate Facebook into my Android Java app, what I want it
I have a fair amount of Facebook development experience, but this had made me
I have a facebook dialog in the screen in which I put a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.