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