Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Facebook Utility class to determine if logged in or not
#import "UWFacebookService.h"
@implementation UWFacebookService
// Static
static const int ddLogLevel = LOG_LEVEL_DEBUG;
// Strong
@synthesize facebookGraphUser = _facebookGraphUser;
#pragma mark - Inquiries
- (BOOL)isSessionStateEffectivelyLoggedIn:(FBSessionState)state {
BOOL effectivelyLoggedIn;
switch (state) {
case FBSessionStateOpen:
log4Info(@"Facebook session state: FBSessionStateOpen");
effectivelyLoggedIn = YES;
break;
case FBSessionStateCreatedTokenLoaded:
log4Info(@"Facebook session state: FBSessionStateCreatedTokenLoaded");
effectivelyLoggedIn = YES;
break;
case FBSessionStateOpenTokenExtended:
log4Info(@"Facebook session state: FBSessionStateOpenTokenExtended");
effectivelyLoggedIn = YES;
break;
default:
log4Info(@"Facebook session state: not of one of the open or openable types.");
effectivelyLoggedIn = NO;
break;
}
return effectivelyLoggedIn;
}
/**
* Determines if the Facebook session has an authorized state. It might still need to be opened if it is a cached
* token, but the purpose of this call is to determine if the user is authorized at least that they will not be
* explicitly asked anything.
*/
- (BOOL)isLoggedIn {
FBSession *activeSession = [FBSession activeSession];
FBSessionState state = activeSession.state;
BOOL isLoggedIn = activeSession && [self isSessionStateEffectivelyLoggedIn:state];
log4Info(@"Facebook active session state: %d; logged in conclusion: %@", state, (isLoggedIn ? @"YES" : @"NO"));
return isLoggedIn;
}
/**
* Attempts to silently open the Facebook session if we have a valid token loaded (that perhaps needs a behind the scenes refresh).
* After that attempt, we defer to the basic concept of the session being in one of the valid authorized states.
*/
- (BOOL)isLoggedInAfterOpenAttempt {
log4Debug(@"FBSession.activeSession: %@", FBSession.activeSession);
// If we don't have a cached token, a call to open here would cause UX for login to
// occur; we don't want that to happen unless the user clicks the login button over in Settings, and so
// we check here to make sure we have a token before calling open
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
log4Info(@"We have a cached token, so we're going to re-establish the login for the user.");
// Even though we had a cached token, we need to login to make the session usable:
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
log4Info(@"Finished opening login session, with state: %d", status);
}];
}
else {
log4Info(@"Active session wasn't in state 'FBSessionStateCreatedTokenLoaded'. It has state: %d", FBSession.activeSession.state);
}
return [self isLoggedIn];
}
@end
#import "UWFacebookService.h"
@implementation UWFacebookService
// Static
static const int ddLogLevel = LOG_LEVEL_DEBUG;
// Strong
@synthesize facebookGraphUser = _facebookGraphUser;
#pragma mark - Inquiries
- (BOOL)isSessionStateEffectivelyLoggedIn:(FBSessionState)state {
BOOL effectivelyLoggedIn;
switch (state) {
case FBSessionStateOpen:
log4Info(@"Facebook session state: FBSessionStateOpen");
effectivelyLoggedIn = YES;
break;
case FBSessionStateCreatedTokenLoaded:
log4Info(@"Facebook session state: FBSessionStateCreatedTokenLoaded");
effectivelyLoggedIn = YES;
break;
case FBSessionStateOpenTokenExtended:
log4Info(@"Facebook session state: FBSessionStateOpenTokenExtended");
effectivelyLoggedIn = YES;
break;
default:
log4Info(@"Facebook session state: not of one of the open or openable types.");
effectivelyLoggedIn = NO;
break;
}
return effectivelyLoggedIn;
}
/**
* Determines if the Facebook session has an authorized state. It might still need to be opened if it is a cached
* token, but the purpose of this call is to determine if the user is authorized at least that they will not be
* explicitly asked anything.
*/
- (BOOL)isLoggedIn {
FBSession *activeSession = [FBSession activeSession];
FBSessionState state = activeSession.state;
BOOL isLoggedIn = activeSession && [self isSessionStateEffectivelyLoggedIn:state];
log4Info(@"Facebook active session state: %d; logged in conclusion: %@", state, (isLoggedIn ? @"YES" : @"NO"));
return isLoggedIn;
}
/**
* Attempts to silently open the Facebook session if we have a valid token loaded (that perhaps needs a behind the scenes refresh).
* After that attempt, we defer to the basic concept of the session being in one of the valid authorized states.
*/
- (BOOL)isLoggedInAfterOpenAttempt {
log4Debug(@"FBSession.activeSession: %@", FBSession.activeSession);
// If we don't have a cached token, a call to open here would cause UX for login to
// occur; we don't want that to happen unless the user clicks the login button over in Settings, and so
// we check here to make sure we have a token before calling open
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
log4Info(@"We have a cached token, so we're going to re-establish the login for the user.");
// Even though we had a cached token, we need to login to make the session usable:
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
log4Info(@"Finished opening login session, with state: %d", status);
}];
}
else {
log4Info(@"Active session wasn't in state 'FBSessionStateCreatedTokenLoaded'. It has state: %d", FBSession.activeSession.state);
}
return [self isLoggedIn];
}
@end
@iwasrobbed

This comment has been minimized.

Copy link

@iwasrobbed iwasrobbed commented Sep 17, 2013

You seem to have copy/pasted the class twice within the same file. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.