iOS應(yīng)用程序的狀態(tài)

  • 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)變化場景

    • 程序第一次啟動(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 backgroundYES,應(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 backgroundYES,應(yīng)用不能運(yùn)行在后臺(tái),進(jìn)入后臺(tái)后會(huì)立即進(jìn)入Not Running):
      Active -> Inactive -> Background -> Suspended -> Not Running
  • 可通過在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")
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容