[iOS] Beginner's Advancement: AppDelegate's Slim

Posted by solon on Tue, 26 May 2020 19:28:06 +0200

Is the code inside an AppDelegate messy?

Are there thousands of lines of code in an AppDelegate?

image.png
AppDelegate serves as the entry to the program.You should have simple code and a clear style so you can read it yourself and with the developers.
So adding a new category to AppDelegate is the easiest and clearest way to do it
image.png
AppDelegate

The entrance to the AppDelegate main program, which invokes classified encapsulation methods in the entrance to the main program for simple and clear purposes such as (initializing windows, initializing various third-party SDK s, network monitoring, automatic login)

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions {
    //Initialize window
    [self initWindow];

    //UMeng Initialization
    [self initUMeng];

    //Initialize app service
    [self initService];

    //Initialize IM
    [[IMManager sharedIMManager] initIM];

    //Initialize user system
    [self initUserManager];

    //sniffer
    [self monitorNetworkStatus];

    return YES;
}

///Program is about to enter the background
- (void)applicationWillResignActive:(UIApplication *)application {
    // Business logic
}

///Program has entered the background
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Business logic
}

///Program Enter Front Desk
- (void)applicationWillEnterForeground:(UIApplication *)application {  
    // Business logic
}

///Program is activated
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Business logic
}

///Called when the program is closed
- (void)applicationWillTerminate:(UIApplication *)application {   
    // Business logic
}
@end

AppDelegate+AppService includes third-party and in-app business implementations to ease entry code pressure

@interface AppDelegate (AppService)

//Initialize Service
-(void)initService;

//Initialize window
-(void)initWindow;

//Initialize UMeng
-(void)initUMeng;

//Initialize user system
-(void)initUserManager;

//Listen for network status
- (void)monitorNetworkStatus;
@end

AppDelegate+PushService Push is handled here (Aurora Push is described below)

// Below iOS10, program calls this method in the foreground
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Business logic
}

// Below iOS10, call this method by manually clicking on the push prompt
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // Business logic
    completionHandler(UIBackgroundFetchResultNewData);
}

#The pragma mark - iOS10 push app opens by performing the following two methods
// This is a callback at the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary *userInfo = notification.request.content.userInfo;
    
    // Handle in Aurora's own way (Aurora Document Writing)
    [JPUSHService handleRemoteNotification:userInfo];
    //Show notifications in the foreground if necessary
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

// Callback when entering from the background
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary *userInfo = response.notification.request.content.userInfo;

    // Handle in Aurora's own way (Aurora Document Writing)
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler();
}

#pragma mark- JPUSHRegisterDelegate
// iOS 10 Foreground Entry
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification   *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Business logic
    completionHandler(UNNotificationPresentationOptionSound); // This method needs to be executed to choose whether to alert the user or not. There are three types of settings to choose from: Badge, Sound, Alert
}

// iOS 10 Enters from Background
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Business logic
    completionHandler();  // The system requires this method to be executed
}

Note: To keep the code in the controller from getting too messy, be sure to remember to encapsulate the code, for yourself and for others


image.png

Topics: network iOS Windows SDK