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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:14:09+00:00 2026-06-04T08:14:09+00:00

I’m trying to create a connection to Facebook however I’m having an issue with

  • 0

I’m trying to create a connection to Facebook however I’m having an issue with the handling of the openUrl.

In the past I’ve been able to add the following in my app delegate class:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{   
    return [[viewController facebook] handleOpenURL:url];
}

Which has worked as I would expect. However, this time I have a slightly different situation in the sense that the viewController is loaded elsewhere in the app. To get around this problem I came up with an idea of creating a new class that is responsible for handling the connection, but can also be accessed from the class where I create the Facebook post.

To explain further here is the relevant code in my app delegate class

.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{
    FacebookConnectionHandler *fbConnHandler = [[FacebookConnectionHandler alloc] init];

    return [[fbConnHandler facebook] handleOpenURL:url];
}

Then here is the code in the FacebookConnectionHandler class:

.h

#import <Foundation/Foundation.h>
#import "Other_ViewController.h"
#import "Facebook.h"

@interface FacebookConnectionHandler : NSObject <FBSessionDelegate>
{
    Other_ViewController *otherView;
    Facebook *facebook;
}

@property(nonatomic, strong)Other_ViewController *otherView;
@property(nonatomic, strong)Facebook *facebook;

+ (id)sharedManager;

@end

.m

#import "FacebookConnectionHandler.h"

@implementation FacebookConnectionHandler
@synthesize otherView;
@synthesize facebook;

static FacebookConnectionHandler *mySingleton = nil;

+ (id)sharedManager
{
    @synchronized(self) 
    {
        if (mySingleton == nil) mySingleton = [[self alloc] init];
    }

    return mySingleton;
}

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

    // Allow the user to create a post
    [self.otherView createFacebookPost];
}

@end

Finally… here is the relevant code in the Other_ViewController class (where the post is being created):

.h

#import "FBConnect.h"

@interface Other_ViewController : UIViewController <FBSessionDelegate>
{
    Facebook *facebook;
}

@property(nonatomic, retain)Facebook *facebook;

- (void)createFacebookPost;

@end

.m

- (void)createFacebookPost
{
    // Create the post
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Blah", @"name",
                                   @"", @"caption",
                                   @"", @"description",
                                   @"http://www.xyz.com", @"link",
                                   @"", @"picture",
                                   nil];

    // Post it to the users feed
    [facebook dialog:@"feed" andParams:params andDelegate:nil];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) 
    {
        case kFacebookButton:
        {
            if (facebook == nil || ![facebook isSessionValid]) 
            {
                // Setup Facebook connection
                facebook = [[Facebook alloc] initWithAppId:@"1111111111" andDelegate:self];

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

                // Set the connection handler
                FacebookConnectionHandler *fbConnectionHandler = [[FacebookConnectionHandler alloc] init];
                fbConnectionHandler.mapView = self;
                fbConnectionHandler.facebook = self.facebook;

                if (![facebook isSessionValid])
                {
                    NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_actions", nil];
                    [facebook authorize:permissions];
                }
            }
            else
            {   
                // Create the post
                [self createFacebookPost];
            }

            break;
        }
        default:
            break;
    }
}

I may be going about this in completely the wrong way and have completely overcomplicated the problem, however I’m new to the whole facebook SDK and at this point I’m really stumped. Please can someone offer a solution?

Note: To be clear, the issue is that the method fbDidLogin is not being called, thus the rest of the code doesn’t get a chance to run.

  • 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-04T08:14:10+00:00Added an answer on June 4, 2026 at 8:14 am

    You’re not using your singleton in the app delegate, you’re creating a new instance of your connect handler class :

    Instead of

    FacebookConnectionHandler *fbConnHandler = [[FacebookConnectionHandler alloc] init];
    return [[fbConnHandler facebook] handleOpenURL:url];
    

    try

    return [[[FacebookConnectionHandler sharedManager] facebook] handleOpenURL:url];
    

    You’re also not using the singleton in your Other_ViewController class.

    If you are going to have the singleton architecture pattern you have to remember to always use the sharedManager and never alloc/init a new one 🙂

    I sometimes make init throw an exception to remind me that there is a singleton method.

    static FacebookConnectionHandler *mySingleton = nil;
    
    - (id)init {
        @throw [NSException exceptionWithName:self.class.description reason:@"Please use the sharedManager, don't make a new one of these!" userInfo:nil];
    }
    
    - (id)initInternal {
        // Put your real init stuff in here
    }
    
    + (id)sharedManager
    {
        @synchronized(self) 
        {
            if (mySingleton == nil) mySingleton = [[self alloc] initInternal];
        }
    
        return mySingleton;
    }
    

    PS Using a separate Facebook class is exactly the way I’ve done it in apps I’ve written before – your architecture is fine 🙂 I would also consider making the Facebook connection hander class responsible for making it’s own Facebook instance instead of the view controller having to do it 🙂

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.