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<JSApplicationStateDelegate> appStateDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationDefaultOrientationDelegate> appDefaultOrientationDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationBackgroundFetchDelegate> backgroundFetchDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationRemoteNotificationsDelegate> remoteNotificationsDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationLocalNotificationsDelegate> localNotificationsDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationStateRestorationDelegate> stateRestorationDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationURLResourceOpeningDelegate> URLResourceOpeningDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationShortcutItemDelegate> shortcutItemDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationHealthDelegate> healthDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationProtectedDataDelegate> protectedDataDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationWatchInteractionDelegate> watchInteractionDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationExtensionDelegate> extensionDelegate;
@property (strong, nonatomic, nullable) id<JSApplicationActivityContinuationDelegate> activityContinuationDelegate;
当需要处理app状态相关的操作时,只要新建一个对象,并遵循JSApplicationStateDelegate协议
在对象的load方法里将该delegate要实现的对象指派给新建的对象
@interface AppDelegate : UIResponder<JSApplicationStateDelegate>
@end
+ (void)load
{
[JSDecoupledAppDelegate sharedAppDelegate].appStateDelegate = [self new];
}
这样就能实现解耦,不同的事件使用新建不同对象遵循不同的协议
网友评论