iOS APP状态说明

我们创建一个iOS新项目的时候,系统会默认创建一个AppDelegate类,并实现UIApplicationDelegate协议的相关方法,代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    DLog(@"%@",self);
    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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
    DLog(@"%@",self);
}


- (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.
    DLog(@"%@",self);
}


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


- (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.
    DLog(@"%@",self);
}


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

当我们在app进入前台后台需要做相关操作时,则可以在这些方法里面写相关代码。下面我们详细解析一下相关代理方法的执行条件和顺序,以下顺序为在iOS12.4系统下测试所得。

一、APP启动和APP退出

iOS APP状态说明_第1张图片
APP启动和APP退出

二、APP进入桌面和从桌面进入APP

iOS APP状态说明_第2张图片
APP进入后台和后台进入APP

三、APP从前台进入应用切换状态和从应用切换状态进入前台

APP应用切换

四、APP在前台显示或隐藏系统控制中心

iOS APP状态说明_第3张图片
显示控制中心

五、APP在前台显示或隐藏推送中心

iOS APP状态说明_第4张图片
显示推送中心

六、测试demo地址

https://github.com/XMSECODE/ESCAPPStateDemo

你可能感兴趣的:(iOS APP状态说明)