在iOS10+ Swift3.0語言中,協議UIApplicationDelegate定義了iOS App的生命周期。 在程序運行時UIApplication實例對象是單例的, 即靜態變量UIApplication.shared。 其它進程或者iOS系統都是通過UIApplicationDelegate的接口函數與當前進程交互。
蘋果文檔里說app進程分為5種狀態:
1、 Not running 未運行, 即應用尚未被啟動。
2、 Inactive 未激活,app在前臺運行(即顯示ViewController界面)但不響應系統事件,但可以運行代碼。
3、 Active 活躍, app在前臺運行并響應系統事件, 用戶正在操作的app都是這個狀態。
4、 Background 后臺, app進程沒有可見的界面,用戶點擊home鍵后便進入這個狀態;也可能是由系統事件將進程從掛起狀態變為后臺狀態;
5、 Suspended 掛起, 顧名思義, iOS系統將進程緩存起來,不運行任何app代碼。當手機剩余內存不足時先回收掛起的進程。
實際測試Swift3.0 app進程, 進程狀態枚舉類只有3個值:
<pre>
public enum UIApplicationState : Int {
case active
case inactive
case background
}
</pre>
Swift支持監聽進程狀態變化的回調函數,弄明白下面這些函數的作用就夠了, 而且英文注釋已經說明了函數的作用。
<pre>
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("application didFinishLaunchingWithOptions \(application.applicationState.rawValue)")
return true
}
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 (application.applicationState.rawValue)")
}
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 (application.applicationState.rawValue)")
}
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 (application.applicationState.rawValue)")
}
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 (application.applicationState.rawValue)")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("applicationWillTerminate (application.applicationState.rawValue)")
}
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
//內存低警告, 可以釋放一些緩存
print("applicationDidReceiveMemoryWarning (application.applicationState.rawValue)")
}
func applicationDidFinishLaunching(_ application: UIApplication) {
print("applicationDidFinishLaunching (application.applicationState.rawValue)")
}
</pre>
進程狀態:0為active, 1為inactive, 2為background 。
點擊桌面icon:
application didFinishLaunchingWithOptions 1
applicationDidBecomeActive 0
點擊Home鍵:
**applicationWillResignActive 0 **
**applicationDidEnterBackground 2 **//在該函數做需要數據持久化,緩存關鍵數據到文件里; 釋放一些不必要的內存空間。
再次點擊桌面icon:
**applicationWillEnterForeground 2 **
applicationDidBecomeActive 0
殺進程:
不執行任何生命周期!!!