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 7636737

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:45:40+00:00 2026-05-31T07:45:40+00:00

I have a logic issue. I need to access an instance of an object

  • 0

I have a logic issue. I need to access an instance of an object from another class.

I have a class called FacebookController. It has several delegate methods in it, for example fbDidLogin. First the facebookloginButtonClicked method gets executed when the user clicks on a button, and after some internal processing, the fbDidLogin method will get called, and the user will log in to the application. (All of this works perfectly).

Now I need to log out from the application. There is a delegate method called logout, and I have to call it as [facebook logout].

I have added a method called -(void) logoutFacebook. When the user clicks the logout button, the following method is called. The logout method is as follows;

-(void) logoutFacebook {
   [facebook logout];    
}

Logout works only if I log out (call logoutFacebook method) from the same viewController.

For instance if I am in a class called Student, I am trying to call the logoutFacebook method of FacebookController. My approach is as follows;

FacebookController *facebookController = [[FacebookController alloc]]init;

[facebookController logoutFacebook];

This doesn’t work, because by doing this, it will create a new instance of FacebookController . So I need to somehow access the original facebook object which was in the FacebookController (which was created after login). How do I get access this object ?

The code:

-(void)facebookloginButtonClicked:(id)sender{
 facebook = [[Facebook alloc] initWithAppId:@"3232232" andDelegate:self];

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

        facebook.accessToken  = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if (![facebook isSessionValid]) {        
        [facebook authorize:nil];      

    }
}
- (void)fbDidLogin {

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

    }

-(void) logoutFacebook {
       [facebook logout];    
    }
  • 0 0 Answers
  • 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-31T07:45:41+00:00Added an answer on May 31, 2026 at 7:45 am

    in FacebookController.m class add this code above the @implementation

    static FacebookController* Object;
    

    add the following function to the .h file

    + (FacebookController *) sharedInstance;       
    

    Implementation of the above function in the .m file

    + (FacebookController *) sharedInstance
    {
    if( Object == nil)
    {
    Object = [[FacebookController alloc]]init;
    }
    
    return Object;
    
    }
    

    Now wherever you want to use the Facebook object ..use it like this

    [FacebookController sharedInstance];
    

    Now you will only have one instance of FacebookController class in your app’s whole lifetime..This is called Singleton pattern..You can find google it to get more detail on it.

    References:

    • https://en.wikipedia.org/wiki/Singleton_pattern
    • https://www.galloway.me.uk/tutorials/singleton-classes/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some logic, which defines and uses some user-defined types, like these: class
I have authentication logic that uses cookies. The model attributes are initialized from cookies,
My application has a restricted access. I have a user/password box in a small
I have issue where I need store info about users in DB and authorize
I have a shopping cart ListView control backed by a custom object that has
I'm sure you've all seen them. Line of Business UIs that have logic such
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
I have some logic in before_save whereby (only) when some conditions are met I
I recently inherited a VBA macro that needs to have validation logic added to

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.