【发布时间】:2019-01-14 10:36:09
【问题描述】:
我正在使用类别将+load 添加到UIResponder,这样我正在组装的 Cocoapod 就可以获得didFinishLaunching 通知,而无需使用 Cocoapod 的人修改他们的appDelegate。
这是个坏主意吗? 这样做会不会有一些我没有想到的后果?
类别:
@implementation UIResponder (MyCategory)
+ (void) load
{
[[NSNotificationCenter defaultCenter] addObserver: [Observer sharedInstance]
selector: @selector(didFinishLaunching:) name: UIApplicationDidFinishLaunchingNotification object: nil];
}
@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
【问题讨论】:
标签: ios objective-c cocoa-touch cocoapods