RamblerAppDelegateProxy 0.0.5

RamblerAppDelegateProxy 0.0.5

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最新发布2017年2月

Vadim SmalEgor TolstoyVadim Smal维护。



  • 作者:
  • Vadim Smal

概览

RamblerAppDelegateProxy 是对抗庞大的 AppDelegate 类的最佳方案。它提供了一种优雅且干净的方式,将所有 AppDelegate 的职责分配给多个辅助类。

Picture

想象一下,你不再有一个包含2k行代码的怪物,而是多个简单的类

  • StartAppDelegate - 在启动时配置应用程序
  • AnalyticsAppDelegate - 配置分析服务
  • RemoteNotificationAppDelegate - 处理推送通知
  • SpotlightAppDelegate - 处理从Spotlight结果启动
  • HandoffAppDelegate - 处理从Handoff启动
  • DeepLinkAppDelegate - 封装深度链接逻辑
  • 以及其他任何内容。

主要功能

  • 提供所有基础设施 - 您只需创建具有单个职责的自己的最小AppDelegate类。
  • 具有简单的依赖注入系统,用于注入您的最小实例
  • 经过与许多Rambler&Co项目的实战测试。

安装

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

作者

Vadim Smal

许可证

MIT