Skip to content

Instantly share code, notes, and snippets.

@danielctull
Created January 2, 2015 16:05
Show Gist options
  • Save danielctull/f5bc4e8ede964478e37c to your computer and use it in GitHub Desktop.
Save danielctull/f5bc4e8ede964478e37c to your computer and use it in GitHub Desktop.
@import Foundation;
typedef void(^DCTAuthPlatformCompletion)(BOOL success);
typedef void(^DCTAuthPlatformURLOpener)(NSURL *URL, DCTAuthPlatformCompletion completion);
typedef void(^DCTAuthPlatformExpirationHandler)();
typedef id(^DCTAuthPlatformBeginBackgroundTaskBlock)(DCTAuthPlatformExpirationHandler expirationHandler);
typedef void(^DCTAuthPlatformEndBackgroundTaskBlock)(id identifier);
@interface DCTAuthPlatform : NSObject
+ (instancetype)sharedPlatform;
@property (nonatomic, copy) DCTAuthPlatformURLOpener URLOpener;
@property (nonatomic, copy) DCTAuthPlatformBeginBackgroundTaskBlock beginBackgroundTaskHandler;
@property (nonatomic, copy) DCTAuthPlatformEndBackgroundTaskBlock endBackgroundTaskHandler;
- (void)openURL:(NSURL *)URL completion:(DCTAuthPlatformCompletion)completion;
- (id)beginBackgroundTaskWithExpirationHandler:(DCTAuthPlatformExpirationHandler)handler;
- (void)endBackgroundTask:(id)identifier;
@end
@implementation DCTAuthPlatform
+ (instancetype)sharedPlatform {
static DCTAuthPlatform *platform;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
platform = [self new];
});
return platform;
}
- (void)openURL:(NSURL *)URL completion:(DCTAuthPlatformCompletion)completion {
if (!completion) {
completion = ^(BOOL success) {};
}
if (!self.URLOpener) {
completion(NO);
return;
}
self.URLOpener(URL, completion);
}
- (id)beginBackgroundTaskWithExpirationHandler:(DCTAuthPlatformExpirationHandler)handler {
if (!self.beginBackgroundTaskHandler) {
return nil;
}
return self.beginBackgroundTaskHandler(handler);
}
- (void)endBackgroundTask:(id)identifier {
if (!self.endBackgroundTaskHandler) {
return;
}
self.endBackgroundTaskHandler(identifier);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment