Shirish Kumar
Shirish Kumar

Reputation: 1532

FaceBook SDK3.5 closeAndClearTokenInformation calls completion handler of openActiveSessionWithReadPermissions

I have the following code that I use during facebook login.

- (BOOL)openFBSessionWithAllowLoginUI:(BOOL)allowLoginUI
            withCompletionHandler:(void (^)())completionHandler
{

    NSArray *permissions = [NSArray arrayWithObjects:
                        @"user_photos",
                        @"email",
                        nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
       if (error != nil) {
        ...
       } else {
            switch (state) {
               case FBSessionStateOpen:
               {
                   ...
               }
               case FBSessionStateClosed:
               {
                   ...
               }
               case FBSessionStateClosedLoginFailed:
               {
                   ...
               }
               default:
                   break;
           }
       }
   }];
}

The above works fine for login. But, when I log out using the following code

[FBSession.activeSession closeAndClearTokenInformation];

this again calls the completionHandler of openActiveSessionWithReadPermissions:permissions allowLoginUI:. That does not make sense to me. I do not think it is the right behavior. Has anyone seen this problem? How do we log-out? I am using SDK 3.5 on iOS6.

Upvotes: 10

Views: 907

Answers (2)

Adam Wallner
Adam Wallner

Reputation: 2411

This is a very bad behavior I think.

FBSession has a hidden property:

@property (readwrite, copy) FBSessionStateHandler loginHandler;

So you can set it to nil by this code in the block like this:

[FBSession openActiveSessionWithReadPermissions:FACEBOOK_PERMISSIONS
                                           allowLoginUI:NO
                                      completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
                                          [[FBSession activeSession] performSelector:NSSelectorFromString(@"setLoginHandler:") withObject:nil];
#pragma clang diagnostic pop
                                          // Your stuff...
                                      }];

Upvotes: 0

Richard D
Richard D

Reputation: 5685

According to this thread on the Facebook Developer bug tracker, this behavior is "by design".

In fact, I did suggest a better name for this method would be: openActiveSessionWithReadPermissions:allowLoginUI:stateChangeHandler:

as that more accurately describes what's happening (the "completionHandler" is in fact called on state change).

You can handle this in several ways: Ben Cohen suggest you can either set the completionHandler to nil within the completion block (to ensure run-once), this answer suggests creating an FBSessionStateHandler run-once handler, or you can switch on the state change.

Ideally, as we rely on the Facebook SDK for specific purposes (log in, log out, make requests, etc.), these would be provided via delegates, but since the SDK developers apparently got a bit carried away with "ooh blocks!!", you're left having to define your state change handler at the point where you first open your session.

Upvotes: 2

Related Questions