Verify that you are logged in to ios facebook sdk 3.0

I am using iOS facebook SDK 3.0. How can I check if a user has already been registered?

I tried the line below, but it does not work properly. It sometimes returns NO, although I logged in. Any suggestions?

if (FBSession.activeSession.isOpen == YES) { // post to wall else login } 

- EDIT -

this is how i open a facebook session:

 NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_likes", @"read_stream", @"publish_actions", nil]; return [FBSession openActiveSessionWithPermissions:permissions allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; 

The first time he needs a login and therefore works. If I try this while I am already registered with FBSession.activeSession.isOpen returns NO.

+6
source share
6 answers

You can check if you have a valid token by trying to open a new session without allowing login UI

 if (FBSession.activeSession.isOpen) { // post to wall } else { // try to open session with existing valid token NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_likes", @"read_stream", @"publish_actions", nil]; FBSession *session = [[FBSession alloc] initWithPermissions:permissions]; [FBSession setActiveSession:session]; if([FBSession openActiveSessionWithAllowLoginUI:NO]) { // post to wall } else { // you need to log the user } } 
+13
source

If you use FBSDK more than 4.x then there is no concept of FBSession. You should find the active session only with [FBSDKAccessToken currentAccessToken] , just check if it is nil , there is no active session yet.

Instead, you should check [FBSDKAccessToken currentAccessToken] for viewDidLoad or the like. If there is a current token , do it after logging in. You can also use currentAccessToken to retrieve cached tokens.

you can find here https://developers.facebook.com/docs/ios/upgrading-4.x

FBSession.activeSession been replaced by [FBSDKAccessToken currentAccessToken] and FBSDKLoginManager . There is no concept of session state . Instead, use the dispatcher to log in, and this sets the currentAccessToken link.

+13
source

I made it like in Facebook

 if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) { } 
+9
source

A session is active if the state is in either FBSessionStateOpen or FBSessionStateOpenTokenExtended . You can use the function below to check if the user is logged in:

 - (BOOL)isSessionOpen { return FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended; } 
+2
source

How do you open your FBSession?

If you are creating an instance, be sure to set FBSession.activeSession. This has been my problem for a while.

+1
source
  if ([FBSDKAccessToken currentAccessToken]) { NSLog(@"Already login"); //[FBSession openActiveSessionWithAllowLoginUI: YES]; } 
0
source

Source: https://habr.com/ru/post/925345/


All Articles