- iOS应用程序一共有五种状态:
![](https://img.haomeiwen.com/i1678135/9b957ecea3dafd52.png)
- 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 background为
YES
,应用不能运行在后台,进入后台后会立即进入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 background为
YES
,应用不能运行在后台,进入后台后会立即进入Not Running):
Active -> Inactive -> Background -> Suspended -> Not Running
- 程序第一次启动(或者被杀掉以后启动):
-
可通过在APPDelegate的回调方法中打印数据,来查看应用状态变化
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")
}
![](https://img.haomeiwen.com/i1678135/0f97d4f16670f48e.jpg)
网友评论