6

I am using iOS facebook SDK 3.0. How can i check if the user is already logged in?

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

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

-- EDIT --

this is how I open my 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 it needs login and so it works. If i try this while I am already logged in the FBSession.activeSession.isOpen returns NO.

gsach
  • 5,715
  • 7
  • 27
  • 42

6 Answers6

13

You can check if you have a valid token by trying to open a new session without allowing the 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
    }
}
Moxy
  • 4,162
  • 2
  • 30
  • 49
  • Just sends me to authenticate every single time. How do you force the sdk to retain the auth token? Permissions match app but FBSessionStateCreatedTokenLoaded is the state. – Oh Danny Boy Dec 19 '12 at 16:35
  • This answer does not work and has issues with the system account versus tokens obtained via the Facebook app or UIWebView – jjxtra Jan 08 '13 at 06:46
  • @PsychoDad The question is for Facebook sdk 3.0 – Moxy Jan 08 '13 at 08:26
10

If you are using FBSDK greater then 4.x then there is no concept of FBSession. You have to find the active session only by using [FBSDKAccessToken currentAccessToken] simply check if it has nil value, no active session else it is.

Instead, you should check [FBSDKAccessToken currentAccessToken] at viewDidLoad or similar. If a current token is available, do the post-login work. You can also use currentAccessToken to retrieve cached tokens.

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

FBSession.activeSession has been replaced with [FBSDKAccessToken currentAccessToken] and FBSDKLoginManager. There is no concept of session state. Instead, use the manager to login and this sets the currentAccessToken reference.

iHulk
  • 4,869
  • 2
  • 30
  • 39
9

i did it as in the Facebook example

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{

}
Kassem
  • 1,481
  • 3
  • 20
  • 42
  • 1
    I am not so sure about this. session.state appears to now be a bit mask. you can also have FBSessionStateOpenTokenExtended state. there is a comment about a session state open bit: Open session state indicating user has logged in or a cached token is available. I think an == compare would not work in all cases. – John Apr 22 '13 at 16:25
2

The session is active if the state is either in FBSessionStateOpen or in 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;
}
Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

How are you opening your FBSession?

If you're creating an instance, be sure to set FBSession.activeSession. That was my issue for a while.

Sahil
  • 1,268
  • 12
  • 19
0
    if ([FBSDKAccessToken currentAccessToken]) 
{
   NSLog(@"Already login");
    //[FBSession openActiveSessionWithAllowLoginUI: YES];
}
Paresh Hirpara
  • 487
  • 3
  • 10
  • Generally speaking answers will be better received if they explain why/how they work instead of just throwing out some code. – David Berry Mar 16 '16 at 16:00