Skip to content

Instantly share code, notes, and snippets.

@numist
Created April 5, 2014 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numist/9988516 to your computer and use it in GitHub Desktop.
Save numist/9988516 to your computer and use it in GitHub Desktop.
Simple category to UIApplication allowing objects to subscribe to application events via a UIApplicationDelegate-like protocol instead of notifications.
#import <UIKit/UIKit.h>
@protocol NNApplicationSubscriber <NSObject>
@optional
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
- (void)applicationDidEnterBackground:(UIApplication *)application;
- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application;
- (void)applicationProtectedDataDidBecomeAvailable:(UIApplication *)application;
- (void)applicationSignificantTimeChange:(UIApplication *)application;
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation;
- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame;
- (void)applicationWillEnterForeground:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;
@end
@interface UIApplication (DelegateMultiDispatch)
- (void)nn_addSubscriber:(id<NNApplicationSubscriber>)subscriber;
- (void)nn_removeSubscriber:(id<NNApplicationSubscriber>)subscriber;
@end
#import "UIApplication+DelegateMultiDispatch.h"
#import <NNKit/NNKit.h>
static id<NNApplicationSubscriber> subscriberDispatcher;
@implementation UIApplication (DelegateMultiDispatch)
- (void)nn_addSubscriber:(id<NNApplicationSubscriber>)subscriber;
{
if (!subscriberDispatcher) {
[self _nn_initializeDispatcher];
}
if (![self isEqual:[UIApplication sharedApplication]]) {
NSLog(@"Error: Can only add subscribers to the UIApplication object representing the current application.");
return;
}
[(NNMultiDispatchManager *)subscriberDispatcher addObserver:subscriber];
}
- (void)nn_removeSubscriber:(id<NNApplicationSubscriber>)subscriber;
{
if (![self isEqual:[UIApplication sharedApplication]]) {
NSLog(@"Error: Can only add subscribers to the UIApplication object representing the current application.");
return;
}
[(NNMultiDispatchManager *)subscriberDispatcher removeObserver:subscriber];
}
- (void)_nn_initializeDispatcher;
{
subscriberDispatcher = (id<NNApplicationSubscriber>)[[NNMultiDispatchManager alloc] initWithProtocol:@protocol(NNApplicationSubscriber)];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(_nn_applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationDidChangeStatusBarFrame:) name:UIApplicationDidChangeStatusBarFrameNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationDidChangeStatusBarOrientation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationDidFinishLaunching:) name:UIApplicationDidFinishLaunchingNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationDidReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationProtectedDataDidBecomeAvailable:) name:UIApplicationProtectedDataDidBecomeAvailable object:self];
[nc addObserver:self selector:@selector(_nn_applicationProtectedDataWillBecomeUnavailable:) name:UIApplicationProtectedDataWillBecomeUnavailable object:self];
[nc addObserver:self selector:@selector(_nn_applicationSignificantTimeChange:) name:UIApplicationSignificantTimeChangeNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationWillChangeStatusBarOrientation:) name:UIApplicationWillChangeStatusBarOrientationNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationWillChangeStatusBarFrame:) name:UIApplicationWillChangeStatusBarFrameNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:self];
[nc addObserver:self selector:@selector(_nn_applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:self];
}
- (void)_nn_applicationDidBecomeActive:(NSNotification *)notification;
{
[subscriberDispatcher applicationDidBecomeActive:notification.object];
}
- (void)_nn_applicationDidChangeStatusBarFrame:(NSNotification *)notification;
{
NSValue *newFrame = notification.userInfo[UIApplicationStatusBarFrameUserInfoKey];
[subscriberDispatcher application:notification.object didChangeStatusBarFrame:[newFrame CGRectValue]];
}
- (void)_nn_applicationDidChangeStatusBarOrientation:(NSNotification *)notification;
{
NSNumber *orientation = notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey];
[subscriberDispatcher application:notification.object didChangeStatusBarOrientation:[orientation integerValue]];
}
- (void)_nn_applicationDidEnterBackground:(NSNotification *)notification;
{
[subscriberDispatcher applicationDidEnterBackground:notification.object];
}
- (void)_nn_applicationDidFinishLaunching:(NSNotification *)notification;
{
[subscriberDispatcher applicationDidFinishLaunching:notification.object];
[subscriberDispatcher application:notification.object didFinishLaunchingWithOptions:notification.userInfo];
}
- (void)_nn_applicationDidReceiveMemoryWarning:(NSNotification *)notification;
{
[subscriberDispatcher applicationDidReceiveMemoryWarning:notification.object];
}
- (void)_nn_applicationProtectedDataDidBecomeAvailable:(NSNotification *)notification;
{
[subscriberDispatcher applicationProtectedDataDidBecomeAvailable:notification.object];
}
- (void)_nn_applicationProtectedDataWillBecomeUnavailable:(NSNotification *)notification;
{
[subscriberDispatcher applicationProtectedDataWillBecomeUnavailable:notification.object];
}
- (void)_nn_applicationSignificantTimeChange:(NSNotification *)notification;
{
[subscriberDispatcher applicationSignificantTimeChange:notification.object];
}
- (void)_nn_applicationWillChangeStatusBarOrientation:(NSNotification *)notification;
{
NSNumber *orientation = notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey];
[subscriberDispatcher application:notification.object willChangeStatusBarOrientation:[orientation integerValue]];
}
- (void)_nn_applicationWillChangeStatusBarFrame:(NSNotification *)notification;
{
NSValue *newFrame = notification.userInfo[UIApplicationStatusBarFrameUserInfoKey];
[subscriberDispatcher application:notification.object willChangeStatusBarFrame:[newFrame CGRectValue]];
}
- (void)_nn_applicationWillEnterForeground:(NSNotification *)notification;
{
[subscriberDispatcher applicationWillEnterForeground:notification.object];
}
- (void)_nn_applicationWillResignActive:(NSNotification *)notification;
{
[subscriberDispatcher applicationWillResignActive:notification.object];
}
- (void)_nn_applicationWillTerminate:(NSNotification *)notification;
{
[subscriberDispatcher applicationWillTerminate:notification.object];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment