在iOS10+ Swift3.0语言中,协议UIApplicationDelegate定义了iOS App的生命周期。 在程序运行时UIApplication实例对象是单例的, 即静态变量UIApplication.shared。 其它进程或者iOS系统都是通过UIApplicationDelegate的接口函数与当前进程交互。
苹果文档上的生命周期配图
苹果文档里说app进程分为5种状态:
1、 Not running 未运行, 即应用尚未被启动。
2、 Inactive 未激活,app在前台运行(即显示ViewController界面)但不响应系统事件,但可以运行代码。
3、 Active 活跃, app在前台运行并响应系统事件, 用户正在操作的app都是这个状态。
4、 Background 后台, app进程没有可见的界面,用户点击home键后便进入这个状态;也可能是由系统事件将进程从挂起状态变为后台状态;
5、 Suspended 挂起, 顾名思义, iOS系统将进程缓存起来,不运行任何app代码。当手机剩余内存不足时先回收挂起的进程。
实际测试Swift3.0 app进程, 进程状态枚举类只有3个值:
<pre>
public enum UIApplicationState : Int {
case active
case inactive
case background
}
</pre>
Swift支持监听进程状态变化的回调函数,弄明白下面这些函数的作用就够了, 而且英文注释已经说明了函数的作用。
<pre>
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("application didFinishLaunchingWithOptions \(application.applicationState.rawValue)")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
print("applicationWillResignActive (application.applicationState.rawValue)")
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print("applicationDidEnterBackground (application.applicationState.rawValue)")
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
print("applicationWillEnterForeground (application.applicationState.rawValue)")
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print("applicationDidBecomeActive (application.applicationState.rawValue)")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("applicationWillTerminate (application.applicationState.rawValue)")
}
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
//内存低警告, 可以释放一些缓存
print("applicationDidReceiveMemoryWarning (application.applicationState.rawValue)")
}
func applicationDidFinishLaunching(_ application: UIApplication) {
print("applicationDidFinishLaunching (application.applicationState.rawValue)")
}
</pre>
进程状态:0为active, 1为inactive, 2为background 。
点击桌面icon:
application didFinishLaunchingWithOptions 1
applicationDidBecomeActive 0
点击Home键:
**applicationWillResignActive 0 **
**applicationDidEnterBackground 2 **//在该函数做需要数据持久化,缓存关键数据到文件里; 释放一些不必要的内存空间。
再次点击桌面icon:
**applicationWillEnterForeground 2 **
applicationDidBecomeActive 0
杀进程:
不执行任何生命周期!!!
网友评论