AppDelegateDispatcher
受https://medium.com/ios-os-x-development/pluggableapplicationdelegate-e50b2c5d97dd的启发,AppDelegateDispatcher用于解耦AppDelegate与服务。
安装
pod 'AppDelegateDispatcher'
如何使用?
- 让您的服务遵守协议
AppService
- 在服务中,实现您想覆盖的方法。
@interface Service1 : NSObject <AppService>
@end
- 让AppDelegate继承自AppDelegateDispatcher
@interface AppDelegate : AppDelegateDispatcher
@property (strong, nonatomic) UIWindow *window;
@end
- 在AppDelegate.m中,创建服务的实例。
@implementation AppDelegate
- (instancetype)init {
self = [super init];
if (self) {
self.services = @[[Service1 new],
[Service2 new],
[Service3 new]];
}
return self;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
@end