Skip to content

Instantly share code, notes, and snippets.

@samjt
Created January 10, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samjt/5ebb6e444310ed39b2b2e579bb939b49 to your computer and use it in GitHub Desktop.
Save samjt/5ebb6e444310ed39b2b2e579bb939b49 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@samjt
Copy link
Author

samjt 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