Facebook iOS seems to be a lost session when you return to the application after clicking the "OK" button so that you already authorize the application

I updated my application from Facebook SDK 2 to 3.1.

I have a Post button on RecordsScreen. If the user has not connected to Facebook, the login page will appear, and if the user is already logged in, another one will be downloaded to ask the user to enter a message for publication on his own wall.

I already have a login. And after that, every time I click the Publish button, the page about this application is already authorized and asks whether to continue by clicking OK. After clicking the "OK" button, the application terminates and starts from the very beginning. Each time I click the Create button, this page reappears. It looks like he cannot find a valid session or a token is lost.

I tested this on Simulator and device. The same thing is happening. The deployment objective is iOS5.1.

The only parameter I did not enter is the iPhone App Store identifier. Will this affect the behavior above?

I tried many times and could not find a solution.

Any help is appreciated.

Thanks!

AOB


Edited January 4:

I found out that openSessionWithAllowLoginUI is called every time. The state FBSessionStateClosedLoginFailed is returned. There is a problem with the login. But the user has a login from the moment the "Authorization" page appears.

The reason is because applicationWillTerminate runs immediately after openSessonWithAllowLoginUI. Can anyone shed some light on this?


In appDelegate.h I import 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]; } 

MainViewController is a multi-view controller in which tips will switch.

I implemented the FBHandler class to control Facebook I / O. FBHandler.h contains #import "Facebook.h", and FBHandler contains a class containing

 -(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 tips is RecordsScreen.xib, which contains Facebook login / logout buttons. In RecordsScreen.m (the place where the user can see his account and enter or exit 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 RecordScreen.m, if the user clicks the login button,

 if (!FBSession.activeSession.isOpen) { // Login to Facebook. [fbHandler openSessionWithAllowLoginUI:YES]; } else { // If user is login, post to wall. [self postScoreToWall]; } 
+6
source share
2 answers

I found a problem. In plist, "Application does not work in the background" is set to YES. Installation for NO solved.

+4
source

In the code that you show, you only get the basic set of read permissions for your session. You must at least call reauthorizeWithPublishPermissions: passing the appropriate permissions.

I had a similar problem, I solved it by changing two things: when I login, I do not use the two-step permission escalation method that Facebook offers, but I'm trying to get the permissions needed to login using openActiveSessionWithPublishPermissions .

You also need to make sure that handleDidBecomeActive: is called at the end of the Facebook login process before trying to make another sdk call. This may not happen if you have a series of blocks that perform the various steps of the process.

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

+2
source

All Articles