title: iOS Facebook SDK - Session auto closes
body: <p>This is my first time using Facebook SDK so maybe the login/permissions flow is not right, though I followed Facebook tutorial. My application just needs to share on the wall, I am using SDK 3.2.1.</p>
  
  <p>Everytime the user taps the share button I use <code>+ (BOOL)openActiveSessionWithReadPermissions:(NSArray*)readPermissions allowLoginUI:(BOOL)allowLoginUI completionHandler:(FBSessionStateHandler)handler</code> method to login.</p>
  
  <p>The very first time asks for e-mail/password combination. The issue I am facing is it will not reuse the stored token afterwards:</p>
  
  <ul>
  <li><p>If the token is stored (user logged to Facebook previously), the app
  will ask for permissions with the "You already authorized app_name",
  then it will most likely ask again when sharing.</p></li>
  <li><p>If <code>openActiveSessionWithReadPermissions</code> is called a second time, it
  won't ask for any permissions.</p></li>
  <li><p>The third time is called it will ask again.</p></li>
  </ul>
  
  <p>I found the session is being automatically closed but I have no idea why. The completion handler shows how <code>FBSessionStateClosed</code> is received just before <code>FBSessionStateOpen</code>. And I guess this is the reason for Facebook SDK to keep asking every other time for approval.</p>
  
  <p><img src="https://i.stack.imgur.com/gWKwz.png" alt="Completion block Session Closed">
  <img src="https://i.stack.imgur.com/opIri.png" alt="Completion block Session Open"></p>
  
  <p>These 2 traces are from the 2nd time <code>openActiveSessionWithReadPermissions</code> is called (you can see how the session is closed and opened again). The next time the method is called, it will just receive a closed session and will ask with the "already authorized" screen.</p>
  
  <p>My code is somewhat like below:</p>
  
  <pre><code>- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
      return [FBSession openActiveSessionWithReadPermissions:nil
                                                allowLoginUI:allowLoginUI
                                           completionHandler:^(FBSession *session,
                                                               FBSessionState state,
                                                               NSError *error) {
                                               [self sessionStateChanged:session
                                                                   state:state
                                                                   error:error];
                                           }];
  }
  
  - (void)sessionStateChanged:(FBSession *)session
                        state:(FBSessionState) state
                        error:(NSError *)error
  {
      switch (state) {
          case FBSessionStateOpen:
              if (!error) {
                  // We have a valid session
                  NSLog(@"User session found");
              }
              break;
          case FBSessionStateClosed:
          case FBSessionStateClosedLoginFailed:
              [FBSession.activeSession closeAndClearTokenInformation];
              break;
          default:
              break;
      }
  
      [[NSNotificationCenter defaultCenter]
       postNotificationName:FBSessionStateChangedNotification
       object:session];
  }
  </code></pre>
  
  <p>I hope someone can help on this one, I am quite frustrated.</p>