Skip to content

Commit

Permalink
Simplifies static open methods of FBSession
Browse files Browse the repository at this point in the history
Summary:
This diff -
* Renames the static open methods to be more clearly about active session
* Returns boolean to simplify UI setup as a result of the operation
* Adds the ability to open in such a way as to enforce no UI (usually for app launch)
* Updates samples accordingly

Test Plan: ran impacted samples, went through the entire login cycle

Reviewers: clang, gregschechte, ayden, mmarucheck

Reviewed By: gregschechte

CC: msdkexp@

Differential Revision: https://phabricator.fb.com/D537878

Task ID: 1188746
  • Loading branch information
Jason Clark committed Aug 2, 2012
1 parent 846ca7a commit 4340241
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 89 deletions.
4 changes: 3 additions & 1 deletion samples/BooleanOGSample/BooleanOGSample/BOGAppDelegate.m
Expand Up @@ -67,7 +67,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// We create and open a session at the outset here; if login is cancelled or fails, the application ignores
// this and continues to provide whatever functionality that it can
NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];
[FBSession sessionOpenWithPermissions:permissions completionHandler:nil];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:nil];

return YES;
}
Expand Down
44 changes: 22 additions & 22 deletions samples/JustRequestSample/JustRequestSample/JRViewController.m
Expand Up @@ -60,28 +60,28 @@ - (void)buttonRequestClickHandler:(id)sender {
// login is integrated with the send button -- so if open, we send
[self sendRequests];
} else {
[FBSession sessionOpenWithPermissions:nil
completionHandler:
^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// if otherwise we check to see if the session is open, an alternative to
// to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen
// property of the session object; the macros are useful, however, for more
// detailed state checking for FBSession objects
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
// send our requests if we successfully logged in
[self sendRequests];
}
}];
[FBSession openActiveSessionWithPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// if otherwise we check to see if the session is open, an alternative to
// to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen
// property of the session object; the macros are useful, however, for more
// detailed state checking for FBSession objects
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
// send our requests if we successfully logged in
[self sendRequests];
}
}];
}
}

Expand Down
30 changes: 14 additions & 16 deletions samples/PlacePickerSample/PlacePickerSample/PPViewController.m
Expand Up @@ -55,22 +55,20 @@ - (void)refresh {
[self searchDisplayController:nil shouldReloadTableForSearchScope:SampleLocationSeattle];
} else {
// if the session isn't open, we open it here, which may cause UX to log in the user
[FBSession sessionOpenWithPermissions:nil
completionHandler:
^(FBSession *session, FBSessionState status, NSError *error)
{
if (!error) {
[self refresh];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
show];
}
}
];
[FBSession openActiveSessionWithPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error) {
[self refresh];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
show];
}
}];
}
}

Expand Down
2 changes: 1 addition & 1 deletion samples/Scrumptious/scrumptious/SCAppDelegate.h
Expand Up @@ -33,6 +33,6 @@ extern NSString *const SCSessionStateChangedNotification;
// The app delegate is responsible for maintaining the current FBSession. The application requires
// the user to be logged in to Facebook in order to do anything interesting -- if there is no valid
// FBSession, a login screen is displayed.
- (void)openSession;
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

@end
18 changes: 8 additions & 10 deletions samples/Scrumptious/scrumptious/SCAppDelegate.m
Expand Up @@ -106,12 +106,13 @@ - (void)sessionStateChanged:(FBSession *)session
}
}

- (void)openSession {
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", @"user_photos", nil];
[FBSession sessionOpenWithPermissions:permissions completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
return [FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}

- (BOOL)application:(UIApplication *)application
Expand Down Expand Up @@ -158,11 +159,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

// FBSample logic
// See if we have a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// Yes, so just open the session (this won't display any UX).
[self openSession];
} else {
// No, display the login page.
if (![self openSessionWithAllowLoginUI:NO]) {
// No? Display the login page.
[self showLoginView];
}

Expand Down
2 changes: 1 addition & 1 deletion samples/Scrumptious/scrumptious/SCLoginViewController.m
Expand Up @@ -37,7 +37,7 @@ - (IBAction)performLogin:(id)sender {
// FBSample logic
// The user has initiated a login, so call the openSession method.
SCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSession];
[appDelegate openSessionWithAllowLoginUI:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
Expand Down
23 changes: 12 additions & 11 deletions src/FBLoginView.m
Expand Up @@ -128,11 +128,11 @@ - (void)initialize {
self.autoresizesSubviews = YES;
self.clipsToBounds = YES;

// if our session has a cached token ready, we open it; note that
// it is important that we open it before wiring is in place to cause KVO, etc.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
[FBSession sessionOpenWithPermissions:self.permissions completionHandler:nil];
}
// if our session has a cached token ready, we open it; note that it is important
// that we open the session before notification wiring is in place
[FBSession openActiveSessionWithPermissions:self.permissions
allowLoginUI:NO
completionHandler:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleActiveSessionSetNotifications:)
Expand Down Expand Up @@ -285,11 +285,10 @@ - (void)wireViewForSessionWithoutOpening:(FBSession *)session {
- (void)wireViewForSession:(FBSession *)session {
[self wireViewForSessionWithoutOpening:session];

// anytime we find that our session is created with an available token
// we open it on the spot
if (self.session.state == FBSessionStateCreatedTokenLoaded) {
[FBSession sessionOpenWithPermissions:self.permissions completionHandler:nil];
}
// open the active session on the spot...
[FBSession openActiveSessionWithPermissions:self.permissions
allowLoginUI:NO // ... but only if a token is at the ready
completionHandler:nil];
}

- (void)unwireViewForSession {
Expand Down Expand Up @@ -335,7 +334,9 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
- (void)buttonPressed:(id)sender {
if (self.session == FBSession.activeSession) {
if (!self.session.isOpen) { // login
[FBSession sessionOpenWithPermissions:self.permissions completionHandler:nil];
[FBSession openActiveSessionWithPermissions:self.permissions
allowLoginUI:YES
completionHandler:nil];
} else { // logout action sheet
NSString *name = self.user.name;
NSString *title = nil;
Expand Down
48 changes: 40 additions & 8 deletions src/FBSession.h
Expand Up @@ -311,8 +311,25 @@ typedef void (^FBSessionReauthorizeResultHandler)(FBSession *session,
This is the simplest method for opening a session with Facebook. Using sessionOpen logs on a user,
and sets the static activeSession which becomes the default session object for any Facebook UI widgets
used by the application. This session becomes the active session, whether open succeeds or fails.
Note, if there is not a cached token available, this method will present UI to the user in order to
open the session via explicit login by the user.
@param allowLoginUI Sometimes it is useful to attempt to open a session, but only if
no login UI will be required to accomplish the operation. For example, at application startup it may not
be disirable to transition to login UI for the user, and yet an open session is desired so long as a cached
token can be used to open the session. Passing NO to this argument, assures the method will not present UI
to the user in order to open the session.
@discussion
Returns YES if the session was opened synchronously without presenting UI to the user. This occurs
when there is a cached token available from a previous run of the application. If NO is returned, this indicates
that the session was not immediately opened, via cache. However, if YES was passed as allowLoginUI, then it is
possible that the user will login, and the session will become open asynchronously. The primary use for
this return value is to switch-on facebook capabilities in your UX upon startup, in the case were the session
is opened via cache.
*/
+ (FBSession*)sessionOpen;
+ (BOOL)openActiveSessionWithAllowLoginUI:(BOOL)allowLoginUI;

/*!
@abstract
Expand All @@ -321,15 +338,30 @@ typedef void (^FBSessionReauthorizeResultHandler)(FBSession *session,
used by the application. This session becomes the active session, whether open succeeds or fails.
@param permissions An array of strings representing the permissions to request during the
authentication flow. A value of nil will indicates basic permissions.
A nil value specifies default permissions.
authentication flow. A value of nil will indicates basic permissions. A nil value specifies
default permissions.
@param allowLoginUI Sometimes it is useful to attempt to open a session, but only if
no login UI will be required to accomplish the operation. For example, at application startup it may not
be desirable to transition to login UI for the user, and yet an open session is desired so long as a cached
token can be used to open the session. Passing NO to this argument, assures the method will not present UI
to the user in order to open the session.
@param handler Many applications will benefit from notification when a session becomes invalid
or undergoes other state transitions. If a block is provided, the FBSession
object will call the block each time the session changes state.
@param handler Many applications will benefit from notification when a session becomes invalid
or undergoes other state transitions. If a block is provided, the FBSession
object will call the block each time the session changes state.
@discussion
Returns true if the session was opened synchronously without presenting UI to the user. This occurs
when there is a cached token available from a previous run of the application. If NO is returned, this indicates
that the session was not immediately opened, via cache. However, if YES was passed as allowLoginUI, then it is
possible that the user will login, and the session will become open asynchronously. The primary use for
this return value is to switch-on facebook capabilities in your UX upon startup, in the case were the session
is opened via cache.
*/
+ (FBSession*)sessionOpenWithPermissions:(NSArray*)permissions
completionHandler:(FBSessionStateHandler)handler;
+ (BOOL)openActiveSessionWithPermissions:(NSArray*)permissions
allowLoginUI:(BOOL)allowLoginUI
completionHandler:(FBSessionStateHandler)handler;

/*!
@abstract
Expand Down
31 changes: 16 additions & 15 deletions src/FBSession.m
Expand Up @@ -444,25 +444,26 @@ - (void)setUrlSchemeSuffix:(NSString*)newValue {
#pragma mark -
#pragma mark Class Methods

/*!
@abstract
This is the simplest method for opening a session with Facebook. Using sessionOpen logs on a user,
and sets the static activeSession which becomes the default session object for any Facebook UI controls
used by the application.
*/
+ (FBSession*)sessionOpen {
return [FBSession sessionOpenWithPermissions:nil
completionHandler:nil];
+ (BOOL)openActiveSessionWithAllowLoginUI:(BOOL)allowLoginUI {
return [FBSession openActiveSessionWithPermissions:nil
allowLoginUI:allowLoginUI
completionHandler:nil];

}

+ (FBSession*)sessionOpenWithPermissions:(NSArray*)permissions
+ (BOOL)openActiveSessionWithPermissions:(NSArray*)permissions
allowLoginUI:(BOOL)allowLoginUI
completionHandler:(FBSessionStateHandler)handler {
BOOL result = NO;
FBSession *session = [[[FBSession alloc] initWithPermissions:permissions] autorelease];
[FBSession setActiveSession:session];
// we open after the fact, in order to avoid overlapping close
// and open handler calls for blocks
[session openWithCompletionHandler:handler];
return session;
if (allowLoginUI || session.state == FBSessionStateCreatedTokenLoaded) {
[FBSession setActiveSession:session];
// we open after the fact, in order to avoid overlapping close
// and open handler calls for blocks
[session openWithCompletionHandler:handler];
result = session.isOpen;
}
return result;
}

+ (FBSession*)activeSession {
Expand Down
9 changes: 5 additions & 4 deletions src/FBUserSettingsViewController.m
Expand Up @@ -296,10 +296,11 @@ - (void)openSession {

self.attemptingLogin = YES;

[FBSession sessionOpenWithPermissions:self.permissions completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
[FBSession openActiveSessionWithPermissions:self.permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}

#pragma mark Handlers
Expand Down

0 comments on commit 4340241

Please sign in to comment.