iOS之Application的生命周期

应用程序的状态

  • Not running(未运行):程序没启动
  • Inactive(未激活):程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态
  • Active(激活):程序在前台运行而且接收到了事件。这也是前台的一个正常的模式
  • Backgroud(后台):序在后台而且能执行代码,大多数程序进入这个后台后会在在这个状态上停留一会。时间到之后会进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态
  • Suspended(挂起):程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存


int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


argc和argv是为了与C语言保持一致,在这没用到,不详述。

后面两个参数为principalClassName(主要类名)和delegateClassName(委托类名)。

如果principalClassName是nil,那么它的值将从Info.plist中获取,如果Info.plist中没有,则默认为UIApplication。principalClass这个类除了管理整个程序的生命周期之外什么都不做,它只赋值监听事件然后交给delegateClass去做。

delegateClass将在工程新建时实例化一个对象

NSStringFromClass([AppDelegate class])

相当于@"AppDelegate"。




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}
                            
- (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.
    NSLog(@"WillResignActive");
}

- (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.
    NSLog(@"DidEnterBackground");
}

- (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.
    NSLog(@"WillEnterForeground");
}

- (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.
    NSLog(@"DidBecomeActive");
}

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

启动程序

2014-07-28 15:22:39.883 LifeCycle[3024:a0b] didFinishLaunchingWithOptions

2014-07-28 15:22:39.887 LifeCycle[3024:a0b] DidBecomeActive

按下Home键

2014-07-28 15:22:43.130 LifeCycle[3024:a0b] WillResignActive

2014-07-28 15:22:43.131 LifeCycle[3024:a0b] DidEnterBackground

重新点击程序

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] WillEnterForeground

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] DidBecomeActive


各个程序运行状态时代理的回调:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      告诉代理进程启动但还没进入状态保存
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     告诉代理启动基本完成程序准备开始运行
- (void)applicationWillResignActive:(UIApplication *)application
    当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了
- (void)applicationDidBecomeActive:(UIApplication *)application 
     当应用程序入活动状态执行,这个刚好跟上面那个方法相反
- (void)applicationDidEnterBackground:(UIApplication *)application
    当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
- (void)applicationWillEnterForeground:(UIApplication *)application
当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。
- (void)applicationWillTerminate:(UIApplication *)application
当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。
- (void)applicationDidFinishLaunching:(UIApplication*)application
当程序载入后执行

 

1.application:didFinishLaunchingWithOptions:

程序首次已经完成启动时执行,若直接启动,launchOptions中没有数据;否则,launchOptions将包含对应方式的内容(比如从微信中启动节奏大师--)。

 

2.applicationWillResignActive(将进入后台)

程序将要失去Active状态时调用,比如按下Home键或有电话信息进来。对应applicationWillEnterForeground(将进入前台),这个方法用来

  • 暂停正在执行的任务;
  • 禁止计时器;
  • 减少OpenGL ES帧率;
  • 若为游戏应暂停游戏;

 

3.applicationDidEnterBackground(已经进入后台)

程序已经进入后台时调用,对应applicationDidBecomeActive(已经变成前台),这个方法用来

  • 释放共享资源;
  • 保存用户数据(写到硬盘);
  • 作废计时器;
  • 保存足够的程序状态以便下次恢复;

 

4.applicationWillEnterForeground(将进入前台)

程序即将进去前台时调用,对应applicationWillResignActive(将进入后台)。这个方法用来撤销applicationWillResignActive中做的改变。

 

5.applicationDidBecomeActive(已经进入前台)

程序已经变为Active(前台)时调用。对应applicationDidEnterBackground(已经进入后台)。若程序之前在后台,最后在此方法内刷新用户界面。

 

6.applicationWillTerminate

程序即将退出时调用。记得保存数据,如applicationDidEnterBackground方法一样。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值