Kyle C
Kyle C

Reputation: 4097

Check for a valid Facebook Session

I was just checking for the state of FBSession

- (BOOL)hasValidSession {
    return (self.session.state == FBSessionStateOpen)
 }

Occasionally the state would briefly be changed to FBSessionStateOpenTokenExtended and this would return NO which would bring down my login modal. When I would click connect again it would crash the app for trying to reestablish an active facebook session. So I changed it to

- (BOOL)hasValidSession {
if (self.session.state == FBSessionStateOpen || self.session.state == FBSessionStateOpenTokenExtended) {
    return YES;
}
else {
    return NO;
}}

My above method works so far but it seems like a hack... What is the best ubiquitous way to check for a valid session in your app?

Upvotes: 1

Views: 2704

Answers (2)

Hemang
Hemang

Reputation: 27050

The session is active if the state is either in FBSessionStateOpen or in FBSessionStateOpenTokenExtended. You can use the method below to get the current status of the user is logged in:

- (BOOL)isActiveSession
{
    return (FBSession.activeSession.state == FBSessionStateOpen 
           || FBSession.activeSession.state == FBSessionStateOpenTokenExtended);
}

Upvotes: 0

Axort
Axort

Reputation: 2024

This is the way I check the facebook session in my app and it works for me:

-(BOOL)checkFacebookSession
{
    if([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
    {
        NSLog(@"Logged in to Facebook");
        [self openFacebookSession];
        UIAlertView *alertDialog;

        alertDialog = [[UIAlertView alloc] initWithTitle:@"Facebook" message:@"You're already logged in to Facebook" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

        [alertDialog show];

        [alertDialog release];
        return YES;
    }
    else{
        NSLog(@"Not logged in to Facebook");
        return NO; //Show login flow.
    }
}

Hope it helps! :D

Upvotes: 3

Related Questions