Search code examples
iphoneiosfacebookipadfbconnect

Facebook SDK method 'fbDidLogin' never gets called after user authentication (iOS)


I'm having a lot of trouble posting comments to Facebook. Here is how I have got the code setup; in my viewController class I have set up a button that when pressed checks if the user is connected to Facebook. If they're not connected I attempts to make a connection. Here is the code the .h and .m files:

.h file

#import <UIKit/UIKit.h>
#import "FBConnect.h"

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

@property(nonatomic, retain)Facebook *facebook;

- (IBAction)shareButtonPressed:(id)sender;
- (void)setUpFacebookConnection;

.m file

- (IBAction)shareButtonPressed:(id)sender 
{
    if (facebook == nil) 
    {
        // Setup Facebook connection
        [self setUpFacebookConnection];
    }
}


- (void)setUpFacebookConnection
{
    facebook = [[Facebook alloc] initWithAppId:@"xxxxxx" 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])
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"publish_actions",
                                nil];
        [facebook authorize:permissions];
    }
}


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


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

When I run this code and press the button it opens up safari with a page allowing the user to choose whether to allow the app access to Facebook. Once you accept this it switched back to my app, however the method fbDidLogin never gets called so I can't ever post any messages.

Please can someone help me out, I've spent ages trawling the internet for advice but all of the relevant articles are either outdated or they have the connection to Facebook setup in the app delegate class. Whereas in my situation I only want to user to connect when they choose (i.e. when they press the button).


Solution

  • This method:

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

    must be placed in your application delegate. You will have to change the reference to the facebook instance so that it's valid in the app delegate, of course.