Skip to content

Instantly share code, notes, and snippets.

@samjt samjt/Observer.h
Created Jan 10, 2019

Embed
What would you like to do?
Hooking into didFinishLaunching from a library
@interface Observer : NSObject
+ (id)sharedInstance;
- (void)didFinishLaunching:(NSNotification *)notification;
@end
#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] );
}
@end
#import <UIKit/UIKit.h>
#import "Observer.h"
NS_ASSUME_NONNULL_BEGIN
@interface UIResponder (MyCategory)
@end
NS_ASSUME_NONNULL_END
@implementation UIResponder (MyCategory)
+ (void) load
{
[[NSNotificationCenter defaultCenter] addObserver: [Observer sharedInstance]
selector: @selector(didFinishLaunching:) name: UIApplicationDidFinishLaunchingNotification object: nil];
}
@end
@samjt

This comment has been minimized.

Copy link
Owner Author

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

@samjt

This comment has been minimized.

Copy link
Owner Author

commented Jan 30, 2019

Turns out this is a bad idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.