iOS應(yīng)用程序的生命周期包含了5種狀態(tài),Not running(未運(yùn)行),Inactive(未激活),Active(已激活),Background后臺(tái),Suspended掛起5種狀態(tài)

iOS應(yīng)用程序生命周期的各個(gè)狀態(tài)
未運(yùn)行(Not running):應(yīng)用程序未啟動(dòng)
未激活(Inactive):應(yīng)用程序正在 前臺(tái)運(yùn)行,但是無(wú)法接受任何時(shí)間。通常當(dāng)應(yīng)用程序從一個(gè)狀態(tài)進(jìn)入另一個(gè)狀態(tài)時(shí),會(huì)短暫的停留在此狀態(tài),當(dāng)手機(jī)處于鎖屏或者來(lái)電時(shí),應(yīng)用程序一直處于此狀態(tài)!
已激活(active):應(yīng)用程序正在前臺(tái)運(yùn)行,可以接受處理各種事件。
后臺(tái)(Background):應(yīng)用程序切換到后臺(tái),并且還在執(zhí)行某些代碼,應(yīng)用程序進(jìn)入掛起狀態(tài),會(huì)在次狀態(tài)停留一會(huì),可以通過(guò)特殊的代碼請(qǐng)求,讓此狀態(tài)停留的更久!
掛起(suspended):應(yīng)用程序處于后臺(tái),不能執(zhí)行任何代碼。當(dāng)掛起時(shí),程序處于內(nèi)存中!系統(tǒng)決定是否清除內(nèi)存。當(dāng)手機(jī)內(nèi)存不夠的時(shí)候。系統(tǒng)自動(dòng)清除該程序內(nèi)存,給處于前臺(tái)狀態(tài)應(yīng)用程序用。
iOS應(yīng)用程序各個(gè)狀態(tài)的表現(xiàn)
每個(gè)iOS應(yīng)用程序都包含一個(gè)UIApplication對(duì)象,并且通過(guò)UIApplication監(jiān)控應(yīng)用程序生命周期的全過(guò)程。iOS應(yīng)用程序要給UIApplication指定一個(gè)代理對(duì)象,由代理對(duì)象處理UIApplication檢測(cè)應(yīng)用程序的生命周期時(shí)間
// Override point for customization after application launch.
print("didFinishLaunchingWithOptions:當(dāng)應(yīng)用程序載入之后(或者運(yùn)行之后)執(zhí)行該方法");
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:當(dāng)程序?qū)⒁M(jìn)入非活動(dòng)狀態(tài)時(shí),調(diào)用此方法,在此期間程序不接受任何事件或者消息");
}
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:當(dāng)程序推送到后臺(tái)時(shí),調(diào)用此方法。如果想設(shè)置程序進(jìn)入后臺(tái)后想執(zhí)行某些操作,在此方法里添加代碼即可");
}
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:程序從后臺(tái)回到前臺(tái)時(shí),調(diào)取此方法");
}
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:程序進(jìn)入活動(dòng)狀態(tài)時(shí)調(diào)用此方法");
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("applicationWillTerminate:程序?qū)⒁顺鰰r(shí),調(diào)用此方法。一般用來(lái)保存數(shù)據(jù)或者進(jìn)行退出前的清理工作");
}
程序運(yùn)行結(jié)果
