IOS loses FBSession when closing app
facebook-ios-sdk, ios, objective-c
Solution
Thank you both for your comments. Fortunately I finally was able to to track down the issue. What was occurring was that I had move my logic from the root view controller but still had a call to
`[self openSessionWithAllowLoginUI:NO]`
So this meant that I was calling it two times consecutively. Once in the appDelegate and once again in the root view controller. The second call seemed to clear my token automatically logged me out. Once I was able to identify this and remove the functionality from my root view controller everything worked as expected.
Thanks for your help.
Problem
I'm having the hardest time getting this working. I have an IOS app and I'm attempting to integrate to `Facebook SDK 3.1`. A user can elect to log into Facebook if they do I'm caching the token. The token expiration date returned is weeks in a advance and I have everything working correctly, login/logout, return to foreground. However, every time I close the app the `FBSession` is not being retained. I know this because when the app is relaunched the app delegate performs `[self openSessionWithAllowLoginUI:NO]` and attempts to refresh the session, however this always returns null. I've followed other tutorials and other posts and can't seem to see what I'm doing wrong in my app delegate. I'm banging my head against the wall on why I'm losing this session on app close. I've attached my appDelegate. AppDelegate.m ``` #import "AppDelegate.h" @implementation AppDelegate NSString *const FBSessionStateChangedNotification=@"ro.Tag:FBSessionStateChangedNotification"; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self openSessionWithAllowLoginUI:NO]; NSLog(@"%@",[[FBSession activeSession] accessToken]); return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. // We need to properly handle activation of the application with regards to SSO // (e.g., returning from iOS 6.0 authorization dialog or from fast app switching). [FBSession.activeSession handleDidBecomeActive]; } /* * Callback for session changes. */ - (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"); NSLog(@"session %@",session); } break; case FBSessionStateClosed: case FBSessionStateClosedLoginFailed: [FBSession.activeSession closeAndClearTokenInformation]; break; default: break; } [[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification object:session]; if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } /* * Opens a Facebook session and optionally shows the login UX. */ - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { NSArray *permissions = [[NSArray alloc] initWithObjects: @"email", @"user_games_activity", @"user_location", @"user_likes", @"user_birthday", nil]; //NSLog(@"permissions: %@",permissions); return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; } /* * If we have a valid session at the time of openURL call, we handle * Facebook transitions by passing the url argument to handleOpenURL */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // attempt to extract a token from the url return [FBSession.activeSession handleOpenURL:url]; } /* *Logout * */ - (void) closeSession { [FBSession.activeSession closeAndClearTokenInformation]; } @end ```