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 7596145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:50:55+00:00 2026-05-30T21:50:55+00:00

The code below is trying to lazy login to Facebook right before posting a

  • 0

The code below is trying to lazy login to Facebook right before posting a photo, but has an asynchronous problem. In the logs, the after isSessionValid block will appear before the fbDidLogin and then a facebookErrDomain error 10000 will happen (“OAuthException”, “active access token must be used”, etc).

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
if (![appDelegate.facebook isSessionValid]) {
    [appDelegate.facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil]];
}

NSLog(@"after isSessionValid block");

NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               FACEBOOK_APP_ID, @"app_id",
                               imageData, @"source",
                               message,  @"message",
                               nil];

[appDelegate.facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];

Here is the fbDidLogin in MyAppDelegate

- (void)fbDidLogin {
    NSLog(@"fbDidLogin");

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

I realize that the facebook requestWithGraphPath is running before the fbDidLogin on the FBSessionDelegate but not sure the best way to take the code below the after isSessionValid block log statement and have it run inside fbDidLogin?

Question

I would love to have a completionHandler style API like below. Is there an easy way do that? Alternatively, is there good way to add a callback or block to MyAppDelegate that would be called once from fbDidLogin and then removed?

[appDelegate.facebook authorize:array completionHandler:^(BOOL success) {
    // other setup stuff from first example
    [appDelegate.facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
}];

Update

An answer to How to react to asynchronous events (login)? might be what I am looking for.

  • 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-05-30T21:50:56+00:00Added an answer on May 30, 2026 at 9:50 pm

    Here is what I put in MyAppDelegate, which saves off a completionHandler, and either calls with YES or NO depending on login, then sets completionHandler to nil. I’m sure I should put a @try/@catch in there somewhere to ensure completionHandler gets set to nil.

    @interface MyAppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>
    {
        void (^_completionHandler)(BOOL success);
    }
    
    - (void)facebookAuthorizeWithCompletionHandler:(void (^)(BOOL success))completionHandler {
        if (![facebook isSessionValid]) {
            _completionHandler = completionHandler;
            [facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil]];
        } else {
            completionHandler(YES);
        }
    }
    
    - (void)fbDidLogin {
        NSLog(@"fbDidLogin");
        // removed code that saves accessToken/expirationDate to NSUserDefaults
    
        if (_completionHandler) {
            _completionHandler(YES);
            _completionHandler = nil;
        }
    }
    
    - (void)fbDidNotLogin:(BOOL)cancelled {
        NSLog(@"fbDidNotLogin");
    
        if (_completionHandler) {
            _completionHandler(NO);
            _completionHandler = nil;
        }
    }
    

    I called into it using code like this, which seems to work (but I want to reread about Blocks and Variables to make sure I understand the memory management issues).

    [appDelegate facebookAuthorizeWithCompletionHandler:^(BOOL success) {
    
        NSLog(@"in completionHandler, success=%d", success);
    
        if (success) {
            // other setup stuff from first example
            [appDelegate.facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
        }
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the code below but it keeps locking up my
I am trying to port the code below to mootools 1.1 code but am
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
I'm trying to achieve what the code below suggests, but I'm getting the error
In the code below i`m trying to read a list of selected projects and
In the code below I'm trying to load some images and put them in
Afternoon all, Using the code below I'm trying to load what is render by
The code below is what I am trying to use in order to get
I'm trying to use the code below to send messages via System.Net.Mail and am
Note my code below. I am trying to figure out why my data is

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.