Created
January 10, 2019 15:58
-
-
Save samjt/5ebb6e444310ed39b2b2e579bb939b49 to your computer and use it in GitHub Desktop.
Hooking into didFinishLaunching from a library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Observer : NSObject | |
+ (id)sharedInstance; | |
- (void)didFinishLaunching:(NSNotification *)notification; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import "Observer.h" | |
NS_ASSUME_NONNULL_BEGIN | |
@interface UIResponder (MyCategory) | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation UIResponder (MyCategory) | |
+ (void) load | |
{ | |
[[NSNotificationCenter defaultCenter] addObserver: [Observer sharedInstance] | |
selector: @selector(didFinishLaunching:) name: UIApplicationDidFinishLaunchingNotification object: nil]; | |
} | |
@end |
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
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