Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS seems lost session when return to app after clicking Okay to You have already authorized app

I have upgraded my app from Facebook SDK 2 to 3.1.

I have a Post button on the RecordsScreen. If the user is not login to Facebook, the login page will appear and if the user is login already, another nib will be loaded to prompt the user to enter a message for posting to his/her own wall.

I have already login. And after that, every time when I click the Post button, the page about the app is already authorized and ask you whether to continue by clicking Okay appears. After clicking Okay, the app terminates and starts again launching from the beginning. Every time when I click the Post Button, this same page appears again. It look like it cannot find a valid session or the token is lost.

I have tested this on the Simulator and the device. Same thing occurs. Deployment target is iOS5.1.

The only parameter I have not entered is the iPhone App Store ID. Will this affect the above behavior?

I have tried a lot of times already and could not find a solution.

Any help is appreciated.

Thanks!

aobs


Edited on Jan 4:

I found out that every time openSessionWithAllowLoginUI is called. The state FBSessionStateClosedLoginFailed is returned. There is a problem with login. But the user has login already since the Already Authorized page appears.

The reason is applicationWillTerminate executes right after openSessonWithAllowLoginUI. Can anyone shed light on this?


In the appDelegate.h, I import the FacebookSDK/FacebookSDK.h and in appDelegate.m, the following is implemented:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url);
}

-(void)applicationDidBecomeActive:(UIApplication *)application {
[FBSession.activeSession handleDidBecomeActive];
}

-(void)applicationWillTerminate:(UIApplication *)application {
[FBSession.activeSession close];
}

The mainViewController is a multi-view controller that will switch nibs.

I have implemented a class FBHandler to handle Facebook login/logout. FBHandler.h contains #import "Facebook.h" and FBHandler is a class that contains

-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
  switch(state) {
    case FBSessionStateOpen:
        break;
    case FBSessionStateClosed:
    case FBSessionStateClosedLoginFailed:
        [FBSession.activeSession closeAndClearTokenInformation];
    default:
        break;
  }

  [[NSNotificationCenter defaultCenter]postNotificationName:FBSessionStateChangedNotification object:session];

  // If there is an error, display the alert view.  I have skipped this 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];
    }];
}

One of the nibs - RecordsScreen.xib contains the Facebook login/logout buttons. In the RecordsScreen.m (a place where the user can see his/her score and login or logout from Facebook), the following is added to viewDidLoad:

fbHandler = [[FBHandler alloc] init];
[fbHandler openSessionWithAllowLoginUI:NO];

// Also, we will listen to FBSessionStateChangedNotification in viewDidLoad.  Code is skipped here.

// The implementation of sessionStateChanged in RecordsScreen:
-(void)sessionStateChanged:(NSNotification *)notification {
    if (FBSession.activeSession.isOpen) {
        if (self.facebook == nil) {
            self.facebook = [[Facebook alloc]initWithAppId:FBSession.activeSession.appID andDelegate:nil];
            self.facebook.accessToken = FBSession.activeSession.accessToken;
            self.facebook.expirationDate = FBSession.activeSession.expirationDate;
        }
    } else {
        self.facebook = nil;
    }
}   

In RecordsScreen.m, if the user clicks the login in button,

if (!FBSession.activeSession.isOpen) {
    // Login to Facebook.
    [fbHandler openSessionWithAllowLoginUI:YES];
} else {
    // If user is login, post to wall.
    [self postScoreToWall];
}
like image 459
aobs Avatar asked Jan 01 '13 09:01

aobs


1 Answers

I found the problem. In the plist, "Application does not run in background" was set to YES. Setting to NO solved the problem.

like image 56
aobs Avatar answered Oct 14 '22 06:10

aobs