Itβs hard for me to work. I have an iOS app and am trying to integrate into the Facebook SDK 3.1 . The user can choose to enter Facebook if they are caching a token. The expiration date of the token is weeks in progress, and everything works for me correctly, login / logout, return to the foreground. However, each time the application is closed, the FBSession not saved. I know this because when the application restarts, the application delegate executes [self openSessionWithAllowLoginUI:NO] and tries to update the session, however this always returns null. I have been following other tutorials and other posts and don't seem to see what I'm doing wrong in my appβs deletion.
I hit my head against the wall, why am I losing this session in the application. I added the appDelegate app.
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 // (eg, 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
source share