Created
June 27, 2014 10:29
-
-
Save dimazen/04ead34bca7dcfce1b51 to your computer and use it in GitHub Desktop.
FBSession+RACExtension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Facebook-iOS-SDK/FacebookSDK/FBSession.h> | |
@class RACSignal; | |
@interface FBSession (RACExtension) | |
+ (FBSession *)rac_activeSession:(BOOL)cachedOnly; | |
+ (void)rac_reset; | |
+ (RACSignal *)rac_openedSessionSignal; | |
+ (RACSignal *)rac_openedSessionSignal:(BOOL)forced; | |
- (RACSignal *)rac_openSignal; | |
- (RACSignal *)rac_openSignal:(FBSessionLoginBehavior)behavior; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FBSession+RACExtension.h" | |
#import "FBSession+Internal.h" | |
#import <ReactiveCocoa/ReactiveCocoa/ReactiveCocoa.h> | |
@implementation FBSession (RACExtension) | |
#pragma mark - | |
+ (FBSession *)rac_activeSession:(BOOL)cachedOnly { | |
// reopen from local cache | |
if ([FBSession openActiveSessionWithAllowLoginUI:NO] && [FBSession activeSessionIfExists]) { | |
return [FBSession activeSession]; | |
} else if (!cachedOnly) { | |
// create new one | |
FBSession *session = [[FBSession alloc] initWithPermissions:@[@"email", @"public_profile"]]; | |
[FBSession setActiveSession:session]; | |
return session; | |
} | |
return nil; | |
} | |
#pragma mark - Open Session | |
- (RACSignal *)rac_openSignal { | |
return [self rac_openSignal:FBSessionLoginBehaviorUseSystemAccountIfPresent]; | |
} | |
- (RACSignal *)rac_openSignal:(FBSessionLoginBehavior)behavior { | |
if (self.isOpen) return [RACSignal return:self]; | |
return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
[self openWithBehavior:behavior completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { | |
if (status == FBSessionStateClosedLoginFailed || status == FBSessionStateClosed || status == FBSessionStateCreatedOpening) { | |
[session closeAndClearTokenInformation]; | |
[subscriber sendError:error]; | |
} else { | |
[subscriber sendNext:session]; | |
[subscriber sendCompleted]; | |
} | |
}]; | |
return [RACDisposable disposableWithBlock:^{ | |
[self setStateChangeHandler:NULL]; | |
}]; | |
}] deliverOn:[RACScheduler mainThreadScheduler]] replayLazily]; | |
} | |
#pragma mark - Opened Session | |
+ (RACSignal *)rac_openedSessionSignal { | |
return [self rac_openedSessionSignal:YES]; | |
} | |
+ (RACSignal *)rac_openedSessionSignal:(BOOL)forced { | |
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
[subscriber sendNext:[FBSession rac_activeSession:!forced]]; | |
[subscriber sendCompleted]; | |
return nil; | |
}] flattenMap:^RACStream *(FBSession *session) { | |
if (!session) return [RACSignal return:nil]; | |
return [session rac_openSignal:FBSessionLoginBehaviorUseSystemAccountIfPresent]; | |
}]; | |
} | |
#pragma mark - Reset | |
+ (void)rac_reset { | |
[[FBSession rac_activeSession:NO] closeAndClearTokenInformation]; | |
[FBSession setActiveSession:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment