- iOS應(yīng)用程序一共有五種狀態(tài):
- Not Running 程序還沒運(yùn)行
- Inactive 程序運(yùn)行在foreground但沒有接收事件
- Active 程序運(yùn)行在foreground接收事件
- Background 程序運(yùn)行在background正在執(zhí)行代碼
- Suspended 程序運(yùn)行在background沒有執(zhí)行代碼
-
iOS應(yīng)用程序狀態(tài)變化會(huì)回調(diào)APPDelegate中的方法只厘,但不是每一種狀態(tài)變化都會(huì)有對(duì)應(yīng)的方法(上圖的紅框的兩個(gè)變化就沒有對(duì)應(yīng)的方法)
-
application:didFinishLaunchingWithOptions:
Not Running -> Inactive -
applicationDidBecomeActive:
Inactive -> Active -
applicationWillResignActive:
Active -> Inactive -
applicationDidEnterBackground:
Background -> Suspended -
applicationWillEnterForeground:
Background -> Inactive -
applicationWillTerminate:
Suspended -> Not Running
-
-
常見的應(yīng)用狀態(tài)變化場(chǎng)景
- 程序第一次啟動(dòng)(或者被殺掉以后啟動(dòng)):
Not Running -> Inactive -> Active - 點(diǎn)擊Home鍵(沒有在Inof.plist中設(shè)置Application does not run in background):
Active -> Inactive -> Background -> Suspended - 點(diǎn)擊Home鍵(在Inof.plist中設(shè)置Application does not run in background為
YES
颁井,應(yīng)用不能運(yùn)行在后臺(tái)披蕉,進(jìn)入后臺(tái)后會(huì)立即進(jìn)入Not Running):
Active -> Inactive -> Background -> Suspended -> Not Running - 掛起重新運(yùn)行
Suspended -> Background -> Inactive -> Active - 內(nèi)存清除(殺掉應(yīng)用或刪除應(yīng)用)
Suspended -> Not Running - 應(yīng)用之間的切換
Active -> Inactive
Inactive -> Active - 點(diǎn)擊Home鍵(在Inof.plist中設(shè)置Application does not run in background為
YES
棕所,應(yīng)用不能運(yùn)行在后臺(tái)消返,進(jìn)入后臺(tái)后會(huì)立即進(jìn)入Not Running):
Active -> Inactive -> Background -> Suspended -> Not Running
- 程序第一次啟動(dòng)(或者被殺掉以后啟動(dòng)):
可通過在APPDelegate的回調(diào)方法中打印數(shù)據(jù)汛聚,來查看應(yīng)用狀態(tài)變化
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print("`application:didFinishLaunchingWithOptions:` Not Running -> Inactive")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("`applicationWillResignActive:` Active -> Inactive")
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("`applicationDidEnterBackground:` Background -> Suspended")
}
func applicationWillEnterForeground(_ application: UIApplication) {
print("`applicationWillEnterForeground:` Background -> Inactive")
}
func applicationDidBecomeActive(_ application: UIApplication) {
print("`applicationDidBecomeActive:` Inactive -> Active")
}
func applicationWillTerminate(_ application: UIApplication) {
print("`applicationWillTerminate:` Suspended -> Not Running")
}