iOS应用程序的生命周期包含了5种状态,Not running(未运行),Inactive(未激活),Active(已激活),Background后台,Suspended挂起5种状态
iOS应用程序生命周期的各个状态
未运行(Not running):应用程序未启动
未激活(Inactive):应用程序正在 前台运行,但是无法接受任何时间。通常当应用程序从一个状态进入另一个状态时,会短暂的停留在此状态,当手机处于锁屏或者来电时,应用程序一直处于此状态!
已激活(active):应用程序正在前台运行,可以接受处理各种事件。
后台(Background):应用程序切换到后台,并且还在执行某些代码,应用程序进入挂起状态,会在次状态停留一会,可以通过特殊的代码请求,让此状态停留的更久!
挂起(suspended):应用程序处于后台,不能执行任何代码。当挂起时,程序处于内存中!系统决定是否清除内存。当手机内存不够的时候。系统自动清除该程序内存,给处于前台状态应用程序用。
iOS应用程序各个状态的表现
每个iOS应用程序都包含一个UIApplication对象,并且通过UIApplication监控应用程序生命周期的全过程。iOS应用程序要给UIApplication指定一个代理对象,由代理对象处理UIApplication检测应用程序的生命周期时间
// Override point for customization after application launch.
print("didFinishLaunchingWithOptions:当应用程序载入之后(或者运行之后)执行该方法");
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:当程序将要进入非活动状态时,调用此方法,在此期间程序不接受任何事件或者消息");
}
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:当程序推送到后台时,调用此方法。如果想设置程序进入后台后想执行某些操作,在此方法里添加代码即可");
}
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:程序从后台回到前台时,调取此方法");
}
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:程序进入活动状态时调用此方法");
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("applicationWillTerminate:程序将要退出时,调用此方法。一般用来保存数据或者进行退出前的清理工作");
}