ios objective-c 生命周期

来源:互联网 发布:淘宝模特米雪儿 编辑:程序博客网 时间:2024/05/20 06:41

AppDelegate.m文件中的生命周期方法

////  AppDelegate.m//  Hello_oc////  Created by ybs on 2017/2/25.//  Copyright  2017年 ybs. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    NSLog(@"111didFinishLaunchingWithOptions:%@\n",@"应用启动,并进行初始化时调用,,这个阶段会实例化根视图控制器");    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.    NSLog(@"222applicationWillResignActive:%@\n",@"应用程序从活动状态进入非活动状态时调用,,这个阶段可以保存UI状态");}- (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(@"333applicationDidEnterBackground:%@\n",@"应用进入后台时调用该方法,并发出通知,,这个阶段可以保存用户数据,释放一些资源");}- (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.    NSLog(@"444applicationWillEnterForeground:%@\n",@"应用程序进入前台并没有处于活动状态时调用该方法并发出通知,这个阶段可以恢复用户数据");}- (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(@"555applicationDidBecomeActive:%@\n",@"应用进入前台,并处于活动状态时调用,,这个阶段可以恢复UI状态");}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.    NSLog(@"666applicationWillTerminate:%@",@"应用被终止时调用该方法,并发出通知,这个阶段可以释放一些资源,也可以保存用户数据");}@end

以下为执行及打印结果展示:

//第一次启动程序时,执行://两个阶段,三个状态://Not running-->Inactive-->Active2017-02-25 16:49:30.756 Hello_oc[1787:93854] 111didFinishLaunchingWithOptions:应用启动,并进行初始化时调用,,这个阶段会实例化根视图控制器2017-02-25 16:49:30.877 Hello_oc[1787:93854] 555applicationDidBecomeActive:应用进入前台,并处于活动状态时调用,,这个阶段可以恢复UI状态//当回到桌面状态时,执行:(common+shift+h 返回桌面快捷键,common:键盘上的开始键)//该场景的状态跃迁分两种情况://1.可以在后台运行或挂起,经历3个阶段4个状态:Active-->Inactive-->Background-->Suspended//2.不可以在后台运行或挂起,经历4个阶段5个状态:Active-->Inactive-->Background-->Suspended-->Not running//根据工程属性文件info.plist中的相关属性Application does not run background是与否,可以控制这两种状态//如果文本编辑器打开info.plist文件,该设置项对应的键是UIApplicationExitsOnSuspend2017-02-25 16:49:53.909 Hello_oc[1787:93854] 222applicationWillResignActive:应用程序从活动状态进入非活动状态时调用,,这个阶段可以保存UI状态2017-02-25 16:49:54.507 Hello_oc[1787:93854] 333applicationDidEnterBackground:应用进入后台时调用该方法,并发出通知,,这个阶段可以保存用户数据,释放一些资源2017-02-25 16:50:33.112 Hello_oc[1787:93854] 444applicationWillEnterForeground:应用程序进入前台并没有处于活动状态时调用该方法并发出通知,这个阶段可以恢复用户数据//当再次进入应用程序的时候执行://挂起重新运行场景://经历3个阶段4个状态://Supended-->Background-->Inactive-->Active2017-02-25 16:50:33.112 Hello_oc[1787:93854] 444applicationWillEnterForeground:应用程序进入前台并没有处于活动状态时调用该方法并发出通知,这个阶段可以恢复用户数据2017-02-25 16:50:33.635 Hello_oc[1787:93854] 555applicationDidBecomeActive:应用进入前台,并处于活动状态时调用,,这个阶段可以恢复UI状态//内存清楚场景:(应用终止场景)//在内存清除场景下,应用不会调用任何方法,也不会发出任何通知
0 0
原创粉丝点击