在iOS10+ Swift3.0語言中,協(xié)議UIApplicationDelegate定義了iOS App的生命周期凹蜂。 在程序運(yùn)行時(shí)UIApplication實(shí)例對象是單例的赦颇, 即靜態(tài)變量UIApplication.shared边苹。 其它進(jìn)程或者iOS系統(tǒng)都是通過UIApplicationDelegate的接口函數(shù)與當(dāng)前進(jìn)程交互隐轩。
蘋果文檔里說app進(jìn)程分為5種狀態(tài):
1窗慎、 Not running 未運(yùn)行图云, 即應(yīng)用尚未被啟動惯悠。
2、 Inactive 未激活琼稻,app在前臺運(yùn)行(即顯示ViewController界面)但不響應(yīng)系統(tǒng)事件吮螺,但可以運(yùn)行代碼。
3、 Active 活躍鸠补, app在前臺運(yùn)行并響應(yīng)系統(tǒng)事件萝风, 用戶正在操作的app都是這個(gè)狀態(tài)。
4紫岩、 Background 后臺规惰, app進(jìn)程沒有可見的界面,用戶點(diǎn)擊home鍵后便進(jìn)入這個(gè)狀態(tài)泉蝌;也可能是由系統(tǒng)事件將進(jìn)程從掛起狀態(tài)變?yōu)楹笈_狀態(tài)歇万;
5、 Suspended 掛起勋陪, 顧名思義贪磺, iOS系統(tǒng)將進(jìn)程緩存起來,不運(yùn)行任何app代碼诅愚。當(dāng)手機(jī)剩余內(nèi)存不足時(shí)先回收掛起的進(jìn)程寒锚。
實(shí)際測試Swift3.0 app進(jìn)程, 進(jìn)程狀態(tài)枚舉類只有3個(gè)值:
<pre>
public enum UIApplicationState : Int {
case active
case inactive
case background
}
</pre>
Swift支持監(jiān)聽進(jìn)程狀態(tài)變化的回調(diào)函數(shù)违孝,弄明白下面這些函數(shù)的作用就夠了刹前, 而且英文注釋已經(jīng)說明了函數(shù)的作用。
<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) {
//內(nèi)存低警告雌桑, 可以釋放一些緩存
print("applicationDidReceiveMemoryWarning (application.applicationState.rawValue)")
}
func applicationDidFinishLaunching(_ application: UIApplication) {
print("applicationDidFinishLaunching (application.applicationState.rawValue)")
}
</pre>
進(jìn)程狀態(tài):0為active喇喉, 1為inactive, 2為background 校坑。
點(diǎn)擊桌面icon:
application didFinishLaunchingWithOptions 1
applicationDidBecomeActive 0
點(diǎn)擊Home鍵:
**applicationWillResignActive 0 **
**applicationDidEnterBackground 2 **//在該函數(shù)做需要數(shù)據(jù)持久化拣技,緩存關(guān)鍵數(shù)據(jù)到文件里; 釋放一些不必要的內(nèi)存空間撒踪。
再次點(diǎn)擊桌面icon:
**applicationWillEnterForeground 2 **
applicationDidBecomeActive 0
殺進(jìn)程:
不執(zhí)行任何生命周期9А!制妄!