应用程序的生命周期

//  AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

//UIResponder是XCode5.0以后出现的新类
//新的iOS 将iOS的响应事件分为3种
//将三种响应事件封装成一个新的类 UIResponder
/*
 touch 触摸事件
 remote 远程操控事件
 motion 摇晃事件
 */
//OC或者iOS所有类(自定义类和系统类)的父类都是NSObject

//UIKit中部分控件的直接父类是UIView UIView的父类是UIResponder UIResponder的父类是NSObject

//所以UIKit中所有控件的直接或者间接的父类就是NSObject

//strong是XCode5.0以后出现的新的属性修饰符(ARC的存在 与strong对应的weak)

//strong表示的是强引用 作用和retain相同 所以strong修饰的成员变量也需要管理内存


@property (strong, nonatomic) UIWindow *window;

//UIWindow是窗口类
//所有视图都是在窗口上显示的

@end
//  AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate
//下面的方法是应用程序生命周期必经的几个方法(创建到关闭)
//从main函数向AppDelegate类跳转的时候 最先调用的就是该方法
//只要应用程序被打开 就会调用该方法 并且只要应用程序不关闭 后面就不会再调用该方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions:应用程序打开时调用一次");
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"applicationWillResignActive:应用程序将要进入非活跃状态");
    // 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
{
    NSLog(@"applicationDidEnterBackground:应用程序已经进入后台");
    // 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
{
    NSLog(@"applicationWillEnterForeground:应用程序将要进入前台");
    // 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
{
    NSLog(@"applicationDidBecomeActive:应用程序进入前台已经变为活跃状态");
    // 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
{
    //应用程序如果占用活跃内存非常大的时候 系统会强迫应用程序关闭
    
    //所以在应用程序关闭之前 系统会先调用该方法 所以在程序关闭之前 可以在该方法中 写一些内存补救代码
    NSLog(@"内存紧张或者意外终止调用该方法");
    
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
原文地址:https://www.cnblogs.com/sayimba/p/5673011.html