APP的生命周期關鍵的是要知道你的應用程序是否正在前臺或后臺運行。由于系統資源在iOS 設備上較為有限,一個應用程序必須在后臺與前臺有不同的行為。操作系統也會限制你的應用程序在后臺的運行,以提高電池壽命,并提高用戶與前臺應用程序的體驗。同時對于開發人員來說,熟悉APP在任何時候處于什么樣的狀態也至關重要,熟悉APP所處的狀態才能在合適的時機處理相應的事情。
下面就讓我們一起來看一下APP的整個生命周期都發了些什么是吧~
一、應用程序的狀態
Not running未運行:程序沒啟動。
Inactive未激活:程序在前臺運行,不過沒有接收到事件。在沒有事件處理情況下程序通常停留在這個狀態。
Active激活:程序在前臺運行而且接收到了事件。這也是前臺的一個正常的模式。
Backgroud后臺:程序在后臺而且能執行代碼,大多數程序進入這個狀態后會在在這個狀態上停留一會。
時間到之后會進入掛起狀態(Suspended)。有的程序經過特殊的請求后可以長期處于Backgroud狀態。
Suspended掛起:程序在后臺不能執行代碼。系統會自動把程序變成這個狀態而且不會發出通知。
當掛起時,程序還是停留在內存中的,當系統內存低時,系統就把掛起的程序清除掉,為前臺程序提供更多的內存。
二、程序運行各個狀態時代理的回調函數
1 代理進程啟動但還沒進入狀態保存
1 - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3 NSLog(@"①代理進程啟動但還沒進入狀態保存");
4 return YES;
5 }
2 代理啟動完成程序準備開始運行
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
4
5 NSLog(@"在這里可以進行大量的操作,一般第三方介入配置等各種操作");
6
7 // Override point for customization after application launch.
8
9 self.window.backgroundColor = [UIColor whiteColor];
10 [self.window makeKeyAndVisible];
11 return YES;
12 }
3 應用程序將要入非活動狀態執行,在此期間,應用程序不接收消息或事件,比如信息電話等
1 - (void)applicationWillResignActive:(UIApplication *)application
2 {
3 // 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.
4 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
5 NSLog(@"應用程序將要入非活動狀態執行,在此期間,應用程序不接收消息或事件,比如信息電話等");
6 }
4 應用程序進入活動狀態執行
1 - (void)applicationDidBecomeActive:(UIApplication *)application
2 {
3 // 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.
4 NSLog(@"應用程序進入活動狀態執行");
5 }
5 程序被推送到后臺的時候調用,要設置后臺繼續運行,在這個函數里面設置即可
1 - (void)applicationDidEnterBackground:(UIApplication *)application
2 {
3 // 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.
4 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
5 NSLog(@"要設置后臺繼續運行,在這個函數里面設置即可");
6
7 [application beginBackgroundTaskWithExpirationHandler:^{
8
9 NSLog(@"begin Background Task With Expiration Handler");
10
11 }];
12 }
6 程序從后臺將要重新回到前臺時候調用
1 - (void)applicationWillEnterForeground:(UIApplication *)application
2 {
3 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
4 NSLog(@"程序從后臺將要重新回到前臺時候調用");
5 }
7 程序將要退出時調用,通常是用來保存數據和一些退出前的清理工作。這個需要要設置UIApplicationExitsOnSuspend的鍵值
1 - (void)applicationWillTerminate:(UIApplication *)application
2 {
3 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
4 NSLog(@"程序將要退出時調用");
5 }
8 程序載入后執行
1 - (void)applicationDidFinishLaunching:(UIApplication *)application
2 {
3 NSLog(@"程序載入后執行");
4 }
三、圖解各個過程
1 加載應用程序進入前臺
wKiom1O_WUqCwtFtAAICsTqrSHw795.jpg
2 加載應用程序進入后臺
wKioL1O_WRuT6lqdAAJuq8vGtLI100.jpg
3 基于警告式響應中斷
wKiom1O_WUuzJrzyAADsUVddAyw634.jpg
4 進入后臺運行
wKiom1O_WUzhKVqDAAHMp4k7Hg8691.jpg
5 返回前臺運行
wKioL1O_WR3h45LwAADeSjL5Kkw326.jpg
持續更新~??