JSDecoupledAppDelegate简介

JSDecoupledAppDelegate

JSDecoupledAppDelegate目的是将AppDelegate解耦

首先需要将main.m里的AppDelegate 替换成JSDecoupledAppDelegate
JSDecoupledAppDelegate 也继承了UIResponder,遵循了UIApplicationDelegate
JSDecoupledAppDelegate将UIApplicationDelegate拆分成不同的Delegate

@protocol JSApplicationStateDelegate;
@protocol JSApplicationDefaultOrientationDelegate;
@protocol JSApplicationBackgroundFetchDelegate;
@protocol JSApplicationRemoteNotificationsDelegate;
@protocol JSApplicationLocalNotificationsDelegate;
@protocol JSApplicationStateRestorationDelegate;
@protocol JSApplicationURLResourceOpeningDelegate;
@protocol JSApplicationShortcutItemDelegate;
@protocol JSApplicationHealthDelegate;
@protocol JSApplicationProtectedDataDelegate;
@protocol JSApplicationWatchInteractionDelegate;
@protocol JSApplicationExtensionDelegate;
@protocol JSApplicationActivityContinuationDelegate;

针对每个delegate都有对应的属性

@property (strong, nonatomic, nullable) id appStateDelegate;
@property (strong, nonatomic, nullable) id appDefaultOrientationDelegate;
@property (strong, nonatomic, nullable) id backgroundFetchDelegate;
@property (strong, nonatomic, nullable) id remoteNotificationsDelegate;
@property (strong, nonatomic, nullable) id localNotificationsDelegate;
@property (strong, nonatomic, nullable) id stateRestorationDelegate;
@property (strong, nonatomic, nullable) id URLResourceOpeningDelegate;
@property (strong, nonatomic, nullable) id shortcutItemDelegate;
@property (strong, nonatomic, nullable) id healthDelegate;
@property (strong, nonatomic, nullable) id protectedDataDelegate;
@property (strong, nonatomic, nullable) id watchInteractionDelegate;
@property (strong, nonatomic, nullable) id extensionDelegate;
@property (strong, nonatomic, nullable) id activityContinuationDelegate;

当需要处理app状态相关的操作时,只要新建一个对象,并遵循JSApplicationStateDelegate协议
在对象的load方法里将该delegate要实现的对象指派给新建的对象

@interface AppDelegate : UIResponder

@end

+ (void)load
{
    [JSDecoupledAppDelegate sharedAppDelegate].appStateDelegate = [self new];
}

这样就能实现解耦,不同的事件使用新建不同对象遵循不同的协议

你可能感兴趣的:(JSDecoupledAppDelegate简介)