Instantly share code, notes, and snippets.

What would you like to do?
Hooking into didFinishLaunching from a library
@interface Observer : NSObject
+ (id)sharedInstance;
- (void)didFinishLaunching:(NSNotification *)notification;
#import <Foundation/Foundation.h>
@implementation Observer
+ (id)sharedInstance {
static Observer *bridgeInstance = nil;
@synchronized(self) {
if (bridgeInstance == nil)
bridgeInstance = [[self alloc] init];
return bridgeInstance;
- (void) didFinishLaunching: (NSNotification*) n
UIWindow *appWindow = [[[UIApplication sharedApplication] delegate] window];
NSLog( @"application: %@", [UIApplication sharedApplication] );
NSLog( @"delegate: %@", [[UIApplication sharedApplication] delegate] );
#import <UIKit/UIKit.h>
#import "Observer.h"
@interface UIResponder (MyCategory)
@implementation UIResponder (MyCategory)
+ (void) load
[[NSNotificationCenter defaultCenter] addObserver: [Observer sharedInstance]
selector: @selector(didFinishLaunching:) name: UIApplicationDidFinishLaunchingNotification object: nil];

This comment has been minimized.

Copy link

samjt commented Jan 10, 2019

Using a Category to observe didFinishLaunching and then triggering any set up needed in a singleton, all without modifying AppDelegate directly.
Useful for making libraries/Cocoapods that might need to respond to didFinishLaunching but without having to add them to AppDelegate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment