AppDelegate
is a traditional example of bad code. Lots of line of code that makes so much different things are put together in methods that are called within the application life cycle. But all of those concerns are over.
Using PlugAppDelegate
you decouple AppDelegate from the services that you plug to it. Each ApplicationService
has its own life cycle that is shared with AppDelegate
.
Let see some code.
Here is how a ApplicationService
is like:
#import "LoggerApplicationService.h"
@implementation LoggerApplicationService
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"LoggerApplicationService didFinishLaunchingWithOptions");
return YES;
}
@end
That's all. It is exactly the same as a AppDelegate. Think of ApplicationService
as sub-AppDelegates.
In AppDelegate
you just have to inherit from PluggableApplicationDelegate to register the services.
#import "AppDelegate.h"
#import "LoggerApplicationService.h"
#import "PushApplicationService.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (NSArray<ApplicationService> *)services {
if (![super services]) {
super.services = (NSArray<ApplicationService>*)@[[LoggerApplicationService new],
[PushApplicationService new]];
}
return super.services;
}
@end
Basically, you do an inversion of control. Instead of let AppDelegate instantiate your dependencies, perform actions at every step of its life cycle, you create objects that share the AppDelegate life cycle and plug them into your AppDelegate. Those objects are observers of the AppDelegate. Your AppDelegate has the only responsibility of notify them about its life cycle events.
To run the example project, clone the repo, and run pod install
from the Example directory first.
PluggableApplicationDelegate is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "PlugAppDelegate"
Obj-C version: Sergey Makkena [email protected]
Swift version: fmo91, [email protected] PluggableApplicationDelegate
PlugAppDelegate is available under the MIT license. See the LICENSE file for more info.