Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS SDK 3.1 crashes on subsequent call to FBSession openWithBehavior

Tags:

ios

facebook

sdk

If we call openWithBehavior after a call to closeAndClearTokenInformation, it causes EXC_BAD_ACCESS. Regardless of whether it is using the native iOS built-in dialog or one of the fast-switching ones.

Our code to login to FB, first time through works:

if (![FBSession activeSession]) {
    #ifdef FREE_APP
        NSString* suffix = @"free";
    #else
        NSString* suffix = @"paid";
    #endif
    FBSession *session = [[[FBSession alloc] initWithAppID:@"111111111111111"
                            permissions:permissions
                        urlSchemeSuffix:suffix
                     tokenCacheStrategy:nil] autorelease];
    [FBSession setActiveSession:session];
} 
else if ([FBSession activeSession].isOpen)
    [[FBSession activeSession] close];

[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                     [self sessionStateChanged:session state:state error:error];
                                 }];

Our code to logout, after which the code above fails after openWithBehavior:

[[FBSession activeSession] closeAndClearTokenInformation];

I was initially using openActiveSessionWithReadPermissions instead of openWithBehavior, as prescribed in the 3.1 docs, which does not crash but the app switching back from FB app/Safari did not work. Perhaps because of the need to have a suffix? If it would be easiest to fix the app switching and go back to that, please advise.

Thanks.

like image 458
leontx Avatar asked Nov 05 '12 08:11

leontx


1 Answers

When I ran in the 5.x simulator, I saw an extra, very helpful, error message from openWithBehavior, then looked it up in the source which makes things much more clear:

if (!(self.state == FBSessionStateCreated ||
      self.state == FBSessionStateCreatedTokenLoaded)) {
    // login may only be called once, and only from one of the two initial states
    [[NSException exceptionWithName:FBInvalidOperationException
                             reason:@"FBSession: an attempt was made to open an already opened or closed session"
                           userInfo:nil]
     raise];
}

I've changed my code to always create a fresh session before calling openWithBehavior and it seems happy.

UPDATE: Here's the updated code that checks for an active session, then closes it, before always instantiating a fresh session...

  - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {


      if ([FBSession activeSession])
         [[FBSession activeSession] closeAndClearTokenInformation];

      #ifdef FREE_APP
         NSString* suffix = @"free";
      #else
         NSString* suffix = @"paid";
      #endif

      NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];

      FBSession *session = [[FBSession alloc] initWithAppID:mFacebookID
                                               permissions:permissions
                                           urlSchemeSuffix:suffix
                                        tokenCacheStrategy:nil]; 

      [FBSession setActiveSession:session];

      If (allowLoginUI == YES) {
        NSLog(@"Calling openWithBehavior");
        [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
                                   completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
                                   {
                                      [self sessionStateChanged:session state:state error:error];
                                   }
        ];
     } else if(session.state == FBSessionStateCreatedTokenLoaded) {
        NSLog(@"Calling openWith completion handler");
        [session openWithCompletionHandler:^(FBSession *_session, FBSessionState status, NSError *error)
                                            {   [self sessionStateChanged:session state:status error:error];}
        ];
     }

     [session release];   

     return true;
  }
like image 119
leontx Avatar answered Sep 19 '22 21:09

leontx