APP 的生命周期
先上一張圖, 讓大家有一個直觀的印象, 這圖是我用 keynote 畫的, 第一次使用, 大家多多包涵
下面我就來一一解釋下 APP 的生命周期
-
當我們打開 APP 時, 首先會執行 main函數, 但是與 OC 不同的是, swift 并不能找到 main.swift 這個文件, 這是為什么呢?
當我們在19行打上一個斷點時, 可以發現, swift 當然也執行了 main 函數,只不過,蘋果沒有給我們提供 main 函數的接口, 大概蘋果認為這個反正不常用, 就干脆不單獨設置了吧, 于是就用了@ UIApplicationMain 這么一個關鍵字合并了 APP 入口.
蘋果給出的官方解釋如下:
For iOS apps the default for new iOS project templates is to add @UIApplicationMain to a regular Swift file. This causes the compiler to synthesize a mainentry point for your iOS app, and eliminates the need for a “main.swift” file.
就是說, iOS APP 的默認的項目模板中添加了@UIApplicationMain關鍵字, 這使得編譯器合成了 iOS APP 的 main 入口, 所有就不需要有 main.swift 的存在.
那么在 main函數中, 系統幫我們做了什么事情呢?
系統主要幫我們
1.初始化 UIApplication 對象;
2.設置 applicationDelegate;
3.開啟運行循環,監聽系統事件.
這個運行循環有什么特點呢? 它遵循 FIFO 的原則, 即 First In First Out(先進先出)的原則 , 先監測到的事件先處理, 當沒有監測到事件的時候處于 休眠狀態
當 APP 對象監測到系統事件的時候, 無法處理事件, 這個時候就要用到代理, AppDelegate 這個類遵守了 UIApplicationDelegate 這個代理協議, 并實現了處理 APP 生命周期事件的方法, 這些方法就是第一幅圖中提到的那些方法:
-
程序加載完畢
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. print("applicationDidFinishLaunchingWithOptions") return true }
應用啟動并進行初始化時會調用該方法并發出通知: UIApplicationDidFinishLaunchingNotification.這個階段會實例化根視圖控制器. 也可以寫一些希望在程序加載完畢之后進行的操作.
-
程序獲取焦點
func applicationDidBecomeActive(_ application: UIApplication) { // 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. print("applicationDidBecomeActive") }
應用已經進入前臺, 并處于活動狀態時調用該方法并發出通知 UIApplicationDidBecomeActiveNotification. 這個階段可以重啟任意在程序處于非激活狀態下的暫停的任務(或者是尚未開始的任務), 如果應用程序之前處于后臺, 可以進行刷新 UI 的操作(非必須)
-
程序將要失去焦點(即將被掛起)
func applicationWillResignActive(_ application: UIApplication) { // 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. print("applicationWillResignActive") }
應用即將失去焦點(即將被掛起), 并發出 UIApplicationWillResignActiveNotification 通知, 這個方法是在激活到失活狀態時調用, 比如收到短信或者有電話呼入, 或者當用戶退出運用程序到后臺的轉場時調用. 在這個方法里, 我們需要寫一些暫停當前任務, 暫停計時器, 干掉圖形渲染回調, 終止游戲等代碼
-
程序已經進入后臺
func applicationDidEnterBackground(_ application: UIApplication) { // 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. print("applicationDidEnterBackground") }
應用程序已經進入后臺, 并發出 UIApplicationDidEnterBackgroundNotification 通知, 使用這個方法釋放共享資源, 保存用戶數據, 干掉計時器, 盡可能地儲存當前的應用狀態信息.
-
程序即將進入前臺
func applicationWillEnterForeground(_ application: UIApplication) { // 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. print("applicationWillEnterForeground") }
應用即將進入前臺, 但是還沒有處于活動狀態時調用該方法,并發出 UIApplicationWillEnterForeground通知, 就是這個時候應用還無法響應用戶的點擊的交互事件, 這個階段可以恢復用戶的數據.
-
程序即將終止
func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. print("applicationWillTerminate") }
應用即將被終止時調用, 并發出通知UIApplicationWillTerminate, 這個階段可以保存用戶數據
-
應用程序接收到內存警告
嚴格說來, 這并不是 APP 生命周期的一部分
當應用程序收到系統發出的內存警告時調用, 主要在這里寫一些清除緩存的代碼.
來自:http://www.jianshu.com/p/7f96dc4f4b4f