赞
踩
AppDelegate是管理整个应用程序生命周期的一个类, 之前我还会在这个类中处理很多第三方库的集成和其他功能, 比如:推送/分享/统计/启动页/引导页/广告/Bugly/版本检查/数据更新 等等. 最后导致AppDelegate非常臃肿, 不利于阅读.
最近发现,使用拓展的方式将各个功能的逻辑分离开来,能够让程序结构更清晰更优雅.
- #import "AppDelegate.h"
-
- #import "AppDelegate+WeChat.h"
- #import "AppDelegate+CheckVersion.h"
- #import "AppDelegate+Service.h"
- #import "AppDelegate+AliPush.h"
- #import "AppDelegate+UMAnalysis.h"
- #import "AppDelegate+UMShare.h"
- #import "AppDelegate+ShortcutTouch.h"
- #import "AppDelegate+Score.h"
- #import "AppDelegate+AppSign.h"
- #import "AppDelegate+LaunchAD.h"
- #import "AppDelegate+StartNumber.h"
- #import "AppDelegate+Insterest.h"
- #import "AppDelegate+GuidePage.h"
- #import "AppDelegate+CheckData.h"
- #import "AppDelegate+Pangolin.h"
- #import "AppDelegate+NightMode.h"
- #import "AppDelegate+Bugly.h"
- #import "AppDelegate+ReadTime.h"
-
- #import "CYLTabBarController.h"
-
- #import "IAPManager.h"
- #import <UMShare/UMShare.h>
-
- @interface AppDelegate ()
-
- @end
-
- @implementation AppDelegate
-
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
-
- self.tabBarControllerConfig = [[WXYZ_MainTabbarViewController alloc] init];
- CYLTabBarController *tabBarController = self.tabBarControllerConfig.tabBarController;
- [self.window setRootViewController:tabBarController];
-
- // 检查数据更改
- [self initCheckData];
-
- // 夜览模式
- [self initNightModeState];
-
- // 检查更新
- [self checkVersionCallBack:^(NSUInteger updateState) {}];
-
- // 开启广告
- [self initADManager];
-
- // 启动页
- [self initLaunchADView];
-
- // 引导页
- [self initGuidePageView];
-
- //开启阿里推送
- [self initAliPushWithApplication:application launchOptions:launchOptions];
-
- // 友盟
- [self initUMAnalysis];
-
- // 分享
- [self initUMShare];
-
- // 3D Touch
- [self initShortcutTouch];
-
- // 应用内好评
- [self initAppStoreScore];
-
- // bug统计
- [self initBugly];
-
- // 阅读时长
- [self initReadingTime];
-
- #if WX_Super_Member_Mode || WX_Recharge_Mode
- // 启动IAP
- [[IAPManager shared] startManager];
- #endif
-
- // 微信
- [self registerWeChat];
-
- // 签到
- [self initUserSign];
-
- // 显示沙盒地址
- [self showHome];
-
- return YES;
- }
-
- - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- [self initStartNumber];
- return YES;
- }
-
- - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
- if ([[url host] isEqualToString:@"platformId=wechat"]) {
- return [[UMSocialManager defaultManager] handleOpenURL:url];
- }
- if ([[url host] isEqualToString:@"oauth"]) {
- return [WXApi handleOpenURL:url delegate:self];
- }
- return YES;
- }
-
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
- if ([[UMSocialManager defaultManager] handleOpenURL:url]) {
- return YES;
- }
- return [WXApi handleOpenURL:url delegate:self];
- }
-
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
- if ([[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation]) {
- return YES;
- }
- return [WXApi handleOpenURL:url delegate:self];
- }
-
- - (void)applicationWillResignActive:(UIApplication *)application {
- // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
- }
-
-
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
-
-
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
- }
-
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
- application.applicationIconBadgeNumber = 0;
- if (self.forcedUpdateTitle) {
- [self showUpdateView];
- }
- }
-
- - (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- /**结束IAP工具类*/
- [[IAPManager shared] stopManager];
- }
-
- @end

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。