iOS应用程序生命周期

//这个类代表着UIApplication的生命周期,当应用程序开启退出
//进入后台或者前台会调用这里面的方法
#import "AppDelegate.h"

//类拓展,在里面可以定义属性,在这里面写的属性在编译。h文件的时候不会编译到
@interface AppDelegate ()

@end

@implementation AppDelegate

//应用程序的主入口函数
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"%@",NSStringFromSelector(_cmd)) ;
    

    // 所有的界面都回呈现在window之上。
    // 一般来说,一个程序只有一个window.
    // 我们还没有学习viewController,所以先设置为空。
    self.window.rootViewController = nil ;
    //设置窗体的背景颜色
    //UIColor:颜色类
    self.window.backgroundColor = [UIColor whiteColor] ;
    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.
    //在这个方法里面,我们应该做什么事情
    //停止正在运行的任务,注销定时器,减低OpenGL ES描画帧率。游戏应该停止
    NSLog(@"%@",NSStringFromSelector(_cmd)) ;
}

//应用程序已经进入了后台 有一个知识点进制后台什么什么不支持后台
- (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:方法
}

//应用程序将要进入前台
//后台=》不活跃状态=>前台
- (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(@"%@",NSStringFromSelector(_cmd)) ;
}

//应用程序已经进入前台
- (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.
    //重新开启被停止的任务(或者还没有开启的任务),当应用程序在不活跃状态的时候,如果你的应用程序之前是在后台,重新刷新UI界面
    NSLog(@"%@",NSStringFromSelector(_cmd)) ;
}

//应用程序退出时调用
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    //当程序退出的时候这个方法被调用,保存合适的数据
    
    NSLog(@"%@",NSStringFromSelector(_cmd) );
}

posted on 2015-10-05 10:26  KBAKB  阅读(73)  评论(0)    收藏  举报

导航