应用程序代理AppDelegate解析

应用程序UIApplication是通过代理和外部交互的,当应用程序生命周期中发生改变时UIApplication就会调用代理对应的方法。

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //程序启动之后执行,只在第一次启动后执行,以后不再执行。
    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.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    //程序进入后台后执行,注意进入后台时会先失去焦点再进入后台;
    //分两种情况讨论:
    //1,当你的应用程序不支持后台运行的时候,该函数的作用等同于applicationWillTerminate:,关闭应用程序之前调用,保存上下文环境,释放资源等。
    //2,当应用程序支持后台运行时,该函数的作用是释放无用资源,保存上下文环境,以备应用程序由后台转入前台之后有足够的信息恢复到之前转入后台之前到状态。
    // 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)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)applicationWillTerminate:(UIApplication *)application {
    //程序在终止时执行,包括正常终止或异常终止.
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
原文地址:https://www.cnblogs.com/lxd2502/p/5105775.html