ios应用程序的生命周期

在网易的视频面试中被问到了这个问题。
首先,应用程序有许多种状态如下:

运行状态 运行状态 状态描述
Not running 未运行 程序没有启动
Inactive 未激活 程序在前台运行,不过还未接收到事件
Active 激活 程序在前台运行并且接收到了事件
Background 后台 程序在后台运行一段时间,时间到了之后会进入挂起状态
Suspend 挂起 程序在后台不能执行代码。挂起时程序仍旧停留在内存中,但当系统内存不够时,会将挂起的程序清除掉

下图是状态变化图:

ios应用程序的生命周期_第1张图片
状态变化图

下面是各个代理方法:
当一个APP在开始运行前存在一个启动时间(Launch Time),这个阶段包含两个方法

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

这两个方法一个表示将要完成加载,一个表示已经完成加载了。一般AppDelegate类默认出现 didFinishLaunchingWithOptions
这个方法包含一个参数launchOptions是一个NSDictionary对象。存储了程序启动的原因:

  • 用户点击Icon直接启动程序,launchOptions内无数据。
  • 其他程序通过openURL:方式启动,可以通过键UIApplicationLaunchOptionsURLKey来获取传递过来的URL
  • 由本地通知启动,则可以通过键UIApplicationLaunchOptionsLocalNotificationKey来获取本地通知对象(UILocalNotification)
  • 由远程通知启动,则可以通过键UIApplicationLaunchOptionsRemoteNotificationKey来获取远程通知信息(NSDictionary)

程序launchOptions参数中可能的数值可以参考UIApplication Class Reference的“Launch Options Keys”.
didiFinishLaunchingWithOptions函数返回的是一个BOOL类型,将决定是否处理URL资源,如果返回YES则会由application:openURL:sourceApplication:annotation:方法处理URL(不明白= =)

程序由启动阶段结束后进入运行阶段,程序可能会进入前台运行也可能进入后台运行。下面是两种运行方式的图示:

ios应用程序的生命周期_第2张图片
加载到前台
ios应用程序的生命周期_第3张图片
加载到后台

运行阶段:只靠路直接进入前台
启动阶段执行完后,系统程序状态为“inactive”,进入到运行阶段后会执行下面的方法变为“active”

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // 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.
}

翻译是:当应用程序处在不活动状态时,重新启动一个被暂停(或还未启动)的任务。如果程序之前就在后台,根据情况可以刷新用户界面。

中断情况
有一下几种常见的程序中断情况:

  1. 按下home键或双击home键点击任务栏icon运行其他app
  2. 有来电通知
  3. 有短信或其他app顶部通知或推送

只要存在中断情况首先会执行以下方法:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

应该在该方法中暂停程序的一些延续性操作。
当程序货到active状态应该使用applicationDidBecomeActive:方法将程序恢复运行。
如果程序中断后进入了后台,会调用方法:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 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.
}

翻译是:使用这个方法去释放共享资源,存储用户数据,取消定时器和存储足够的应用程序状态信息以便在终止前恢复到它当前的状态。如果你的应用程序支持后台操作,在用户离开程序后它将代替方法applicationWillTerminate:被调用

中断后,当用户再次返回当前app会调用方法:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

终止阶段

当程序终止时将调用方法:

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

所有的生命周期方法:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // 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.
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

本文参考:http://www.cnblogs.com/wayne23/p/3868441.html

你可能感兴趣的:(ios应用程序的生命周期)