Fork me on GitHub

iOS应用程序的状态

  • iOS应用程序一共有五种状态:

  • Not Running 程序还没运行
  • Inactive 程序运行在foreground但没有接收事件
  • Active 程序运行在foreground接收事件
  • Background 程序运行在background正在执行代码
  • Suspended 程序运行在background没有执行代码
  • iOS应用程序状态变化会回调APPDelegate中的方法,但不是每一种状态变化都会有对应的方法(上图的红框的两个变化就没有对应的方法)
    • application:didFinishLaunchingWithOptions: Not Running -> Inactive
    • applicationDidBecomeActive: Inactive -> Active
    • applicationWillResignActive: Active -> Inactive
    • applicationDidEnterBackground: Background -> Suspended
    • applicationWillEnterForeground: Background -> Inactive
    • applicationWillTerminate: Suspended -> Not Running
  • 常见的应用状态变化场景

    • 程序第一次启动(或者被杀掉以后启动):
      Not Running -> Inactive -> Active
    • 点击Home键(没有在Inof.plist中设置Application does not run in background):
      Active -> Inactive -> Background -> Suspended
    • 点击Home键(在Inof.plist中设置Application does not run in backgroundYES,应用不能运行在后台,进入后台后会立即进入Not Running):
      Active -> Inactive -> Background -> Suspended -> Not Running
    • 挂起重新运行
      Suspended -> Background -> Inactive -> Active
    • 内存清除(杀掉应用或删除应用)
      Suspended -> Not Running
    • 应用之间的切换
      Active -> Inactive
      Inactive -> Active
    • 点击Home键(在Inof.plist中设置Application does not run in backgroundYES,应用不能运行在后台,进入后台后会立即进入Not Running):
      Active -> Inactive -> Background -> Suspended -> Not Running
  • 可通过在APPDelegate的回调方法中打印数据,来查看应用状态变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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")
}

--------------------本文结束👨‍💻感谢您的阅读--------------------
坚持原创技术分享,您的支持将鼓励我继续创作!
  • 本文标题: iOS应用程序的状态
  • 本文作者: AndyRon
  • 发布时间: 2017年07月07日 - 00:00
  • 最后更新: 2019年09月03日 - 12:58
  • 本文链接: http://andyron.com/2017/ios-application-status.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
0%