iosfacebookfacebook-graph-apifacebook-ios-sdkfbconnect

FBSessionDelegate methods not firing


I'm attempting to implement the latest Facebook Connect SDK and I'm having some troubles. For some reason the delegate callbacks for FBSessionDelegate protocol are not being fired. I've followed the instructions on the git Facebook page and tried to mimic the Facebook sample app but no luck. I'm going crazy here so I'm gonna post my code and maybe somebody will see something silly that I've missed.

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

@interface FacebookWrapper : UIViewController <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate>{
 Facebook* _facebook;
 NSArray* _permissions;
}

@property(readonly) Facebook *facebook;

- (void)login;

@end

#import "FacebookWrapper.h"

static NSString* kAppId = @"1234455667778";

@implementation FacebookWrapper

@synthesize facebook = _facebook;

- (id)init {
 if (self = [super init]) {
  _permissions =  [[NSArray arrayWithObjects: @"read_stream", @"offline_access",nil] retain];
  _facebook = [[Facebook alloc] initWithAppId:kAppId];

 }
 return self;
}

- (void)dealloc {
 [_facebook release];
 [_permissions release];
 [super dealloc];
}

- (void)login {
 [_facebook authorize:_permissions delegate:self];
}

- (void)fbDidLogin {
 NSLog(@"Did Log In");
}

- (void)fbDidNotLogin:(BOOL)cancelled {
 NSLog(@"Failed to log in");
}

- (void)fbDidLogout {
 NSLog(@"Logged Out");
}

And to call this from another class,

FacebookWrapper *fw = [[FacebookWrapper alloc] init];
[fw login];

The behavior that I'm seeing on the phone is as expected. The Facebook app launches on init and permissions are requested. The phone then brings my app back to the foreground but the delegates for FBSessionDelegate are never fired. I've tried this in the Facebook sample app using my app ID and it worked fine. I have no idea what the difference is.


Solution

  • I figured out the problem. In the App Delegate you need to override handleOpenURL.

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [[facebookWrapper facebook] handleOpenURL:url];
    }
    

    If you're creating a wrapper class as I am you'll need an instance of it in the app delegate so you can call the hanleOpenURL method in the Facebook class. Also notice that there is a public readonly property for my Facebook instance in my FacebookWrapper class so I can call handlOpenURL.

    By doing this your app will know where to continue when it returns from getting permissions inside of the Facebook app.