【iOS】菜鸟进阶:AppDelegate之瘦身

作者: Laki只是想做一个程序猿 | 来源:发表于2017-12-14 17:58 被阅读112次

是否一个AppDelegate里面的代码杂乱无章?

是否一个AppDelegate里面上千行代码?

image.png
AppDelegate做为程序的入口。应当有简便的代码,清晰明了的风格,以便自己阅读和协同开发者进行阅读。
所以给AppDelegate新增分类,是最简单明了的方法
image.png
AppDelegate

AppDelegate 主程序的入口,在主程序的入口中调用分类封装的方法达到简单明了目的 比如(初始化Window、初始化各种第三方的SDK、网络监听、自动登录)

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

    //UMeng初始化
    [self initUMeng];

    //初始化app服务
    [self initService];

    //初始化IM
    [[IMManager sharedIMManager] initIM];

    //初始化用户系统
    [self initUserManager];

    //网络监听
    [self monitorNetworkStatus];

    return YES;
}

/// 程序即将进入后台
- (void)applicationWillResignActive:(UIApplication *)application {
    // 业务逻辑
}

/// 程序已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 业务逻辑
}

/// 程序进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {  
    // 业务逻辑
}

/// 程序被激活 (挂起)
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 业务逻辑
}

/// 程序关闭时调用
- (void)applicationWillTerminate:(UIApplication *)application {   
    // 业务逻辑
}
@end

AppDelegate+AppService 包含第三方 和 应用内业务的实现,减轻入口代码压力

@interface AppDelegate (AppService)

//初始化服务
-(void)initService;

//初始化 window
-(void)initWindow;

//初始化 UMeng
-(void)initUMeng;

//初始化用户系统
-(void)initUserManager;

//监听网络状态
- (void)monitorNetworkStatus;
@end

AppDelegate+PushService 推送相关在这里处理(以下是介绍极光推送)

// iOS10以下,程序在前台调用此方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // 业务逻辑
}

// iOS10以下,手动点击推送提示调用此方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // 业务逻辑
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark - iOS10推送app打开则执行以下两个方法
// 这是在前台的时候回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary *userInfo = notification.request.content.userInfo;
    
    // 交给极光自己的方法中处理(极光文档写法)
    [JPUSHService handleRemoteNotification:userInfo];
    //如果需要在应用在前台也展示通知
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

// 从后台进入的时候回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary *userInfo = response.notification.request.content.userInfo;

    // 交给极光自己的方法中处理(极光文档写法)
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler();
}

#pragma mark- JPUSHRegisterDelegate
// iOS 10 前台进入
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification   *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // 业务逻辑
    completionHandler(UNNotificationPresentationOptionSound); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}

// iOS 10 从后台进入
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // 业务逻辑
    completionHandler();  // 系统要求执行这个方法
}

注意:为了不让控制器中的代码太杂乱,一定、一定、一定要记得封装代码,为了自己同时也为了他人


image.png

相关文章

网友评论

本文标题:【iOS】菜鸟进阶:AppDelegate之瘦身

本文链接:https://www.haomeiwen.com/subject/jhnkwxtx.html