iOS App进入后台时会发生什么

125 阅读1分钟

根据官方文档(Extending your app’s background execution timeManaging your app’s life cycle)显示

文档查阅时间为2025-11-20

  • 正常情况下,App先进入Background状态,紧接着(时间长短取决于是否通过beginBackgroundTask方法发起任务,但总体都很短)进入Suspended
  • 进入后台时applicationDidEnterBackground会执行,同时进入Background状态,该方法会有5s时间执行其中的任务
  • applicationDidEnterBackground之后,如果没有beginBackgroundTask发起的后台任务,则很快就会进入Suspended状态
  • 进入Suspended状态,App还会在内存中
  • BackgroundSuspended状态的不同点是
    • Background状态下,是可以执行短暂的后台任务
    • Suspended状态下,App代码便不会再执行,收不到任何通知,而且系统可以根据(未其他App腾出内存空间)需要将App终止掉
  • 即使通过beginBackgroundTask开启后台任务也不过最多有30s的执行时间,仅适合执行一些非常重要的任务,比如将一些严重影响用户体验的数据写入磁盘,用作后续状态恢复
  • 如果希望申请更多后台任务执行时间,则需要依赖Background Tasks框架

b0d3522d-c62b-49e2-9f48-01b0a4046b11.png