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

  • Home
  • SEARCH
  • 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 6472421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:20:33+00:00 2026-05-25T06:20:33+00:00

I am making an application where I need to invoke requests in multiple view

  • 0

I am making an application where I need to invoke requests in multiple view controllers. I don’t want the Facebook log in screen to show when the app is launched. It should only open when the user taps a button in another view controller if the user is not logged in, so putting the code in app delegate is not working solution to the problem. And the whole asynchronous process of requesting data in the Facebook API is killing me, I am starting to loose hair.

A class with static methods to get the facebook object and treat it as a singleton.

#import "PBSocialMedia.h"
#import "PBFacebookDelegate.h"

static Facebook *facebook;
static PBFacebookDelegate *facebookDelegate;

@implementation PBSocialMedia

+ (Facebook *)sharedFacebook {

    if (facebook == nil) {
        facebookDelegate = [[PBFacebookDelegate alloc] init];
        facebook = [[Facebook alloc] initWithAppId:@"156687441079334" andDelegate:facebookDelegate];
    }

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

    NSArray *permissions = [NSArray arrayWithObjects:@"user_photo_video_tags", @"friends_photo_video_tags", @"user_photos", @"friends_photos", nil];

    if (![facebook isSessionValid]) {
        [facebook authorize:permissions];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"connectedToFacebook" object:self];
    }

    return facebook;
}

+ (Facebook *)sharedFacebookWithoutAuthorizing {
    return facebook;
}

@end

A class that implements the Facebook session delegate used in the class above

#import "PBFacebookDelegate.h"
#import "PBSocialMedia.h"

@implementation PBFacebookDelegate

/**
 * Called when the user successfully logged in.
 */
- (void)fbDidLogin {
    Facebook *facebook = [PBSocialMedia sharedFacebook];

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

    [[NSNotificationCenter defaultCenter] postNotificationName:@"connectedToFacebook" object:self];
}

/**
 * Called when the user dismissed the dialog without logging in.
 */
- (void)fbDidNotLogin:(BOOL)cancelled {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissedFacebookDialog" object:self];
}

/**
 * Called when the user logged out.
 */
- (void)fbDidLogout {
   [[NSNotificationCenter defaultCenter] postNotificationName:@"disconnectedFromFacebook" object:self];
}


@end

As you see from the two classes above I am making use of notifications so that I know when the user logs in/out of Facebook. The code below is an example of how I am trying to use it:

- (void)viewDidLoad {
    [super viewDidLoad];

    Facebook *facebook = [PBSocialMedia sharedFacebook];

    if (self.canUseGraphAPI) {
        [facebook requestWithGraphPath:@"106378916135129/photos" andDelegate:self];
        NSLog(@"Can use");
    } else {
        NSLog(@"Can't use");
    }
}

- (void)connectedToFacebook {
    self.canUseGraphAPI = YES;
}

- (void)disconnectedFromFacebook {
    self.canUseGraphAPI = NO;
}

This is ofcourse NOT working as intended because of the stupid asynchronous request. It will work correct only when the user is logged in. So the first time I open the app it invokes the [PBSocialMedia sharedFacebook]; Then it proceed with the request function AND NOT waiting for the sharedFacebook function to finish. I really hope somebody has encountered this problem before. I want to do requests in different places in my code, but only when the user is logged in, and it need to WAIT while the user logs in before requesting. The code below is not working but it shows what I want!

- (void)viewDidLoad {
    [super viewDidLoad];

    Facebook *facebook = [PBSocialMedia sharedFacebook];

    while (self.canUseGraphAPI) {
        // WAITING FOR LOGIN IN
    }

    [facebook requestWithGraphPath:@"106378916135129/photos" andDelegate:self];
}

- (void)connectedToFacebook {
    self.canUseGraphAPI = YES;
}

- (void)disconnectedFromFacebook {
    self.canUseGraphAPI = NO;
}
  • 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-25T06:20:34+00:00Added an answer on May 25, 2026 at 6:20 am

    I would start restructuring your code to work by passing in blocks that you want executed upon getting your answer back from FB. This is similar to how you do ajax in JQuery.

    - (void)viewDidLoad {
    [super viewDidLoad];
    
    Facebook *facebook = [PBSocialMedia sharedFacebook];
    
    [self withGraphAPIDo: ^{
        [facebook requestWithGraphPath:@"106378916135129/photos" andDelegate:self];
        }];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an application (iPhone app) which gets information (jokes) from an RSS
I am making an application for 3d ball balancing game on j2me, I need
We are making an application with the details below. 1) Web/Desktop App on C#.Net
I am making an application where I need for a contentEditable div to be
I am making an application where I need to verify the syntax of each
I am making one application in which one webservices need to be called, is
I'm making an Android application where I need to slow down the audio being
I am making an Automator application and I need to replace some text based
I'm making a java application and I need to play audio. I'm playing mainly
I'm making an application in which m using Tab Bars. Now what i need

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.