简体   繁体   中英

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];
}

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

In the code that you are showing you only acquire the basic set of read permissions for your session. There needs to be at least a call to reauthorizeWithPublishPermissions: passing the appropriate permissions.

I just had a similar problem, I resolved it by changing two things, when doing the login I am not using the two step permission escalation method that Facebook suggests but I try to acquire the permissions necessary on the login by using openActiveSessionWithPublishPermissions .

You also need to make sure that handleDidBecomeActive: gets called at the end of the Facebook login process before you try to make another call to the sdk. That might not happen if you have a series of blocks that execute the various steps of this process.

I did not work with the older version of this sdk but it seemed a lot more intuitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM