测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2017年2月 |
由Vadim Smal,Egor Tolstoy和Vadim Smal维护。
RamblerAppDelegateProxy 是对抗庞大的 AppDelegate
类的最佳方案。它提供了一种优雅且干净的方式,将所有 AppDelegate
的职责分配给多个辅助类。
想象一下,你不再有一个包含2k行代码的怪物,而是多个简单的类
StartAppDelegate
- 在启动时配置应用程序AnalyticsAppDelegate
- 配置分析服务RemoteNotificationAppDelegate
- 处理推送通知SpotlightAppDelegate
- 处理从Spotlight结果启动HandoffAppDelegate
- 处理从Handoff启动DeepLinkAppDelegate
- 封装深度链接逻辑AppDelegate
类。pod RamblerAppDelegateProxy
创建一个符合UIApplicationDelegate
协议的RemoteNotificationAppDelegate
类。
@interface RemoteNotificationAppDelegate : NSObject <UIApplicationDelegate>
@end
...
@implementation RemoteNotificationAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(@"Launching from push %@", notification);
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Did register for remote notifications");
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Did receive remote notification");}
}
@end
更改main.m
文件实现
int main(int argc, char * argv[]) {
@autoreleasepool {
[[RamblerAppDelegateProxy injector] addAppDelegates:@[[RemoteNotificationAppDelegate new]]];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([RamblerAppDelegateProxy class]));
}
}
以与标准AppDelegate
类相同的方式使用您的RemoteNotificationAppDelegate
- 只需记住不要混淆它的职责。
根据该理念,代理只转发协议UIApplicationDelegate
中定义的方法。如果您需要实现AppDelegate
的额外方法,则需要创建一个子类并在该类中实现此方法。
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
此子类应设置为默认值。
[[RamblerAppDelegateProxy injector] setDefaultAppDelegate:[AppDelegate new]];
例如,如果您正在使用Typhoon,则需要为您需要依赖的AppDelegates写入RamblerAppDelegateProxy的定义。
@implementation ApplicationAssembly
- (RamblerAppDelegateProxy *)applicationDelegateProxy {
return [TyphoonDefinition withClass:[RamblerAppDelegateProxy class]
configuration:^(TyphoonDefinition *definition) {
[definition injectMethod:@selector(addAppDelegates:)
parameters:^(TyphoonMethod *method) {
NSArray *appDelegates = @[
[self remoteNotificationAppDelegate]
];
[method injectParameterWith:appDelegates];
}];
}];
}
- (id<UIApplicationDelegate>)remoteNotificationAppDelegate {
return [TyphoonDefinition withClass:[RCMLaunchingAppDelegate class]
configuration:^(TyphoonDefinition *definition) {
}];
}
@end
MIT