示例代码:
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"%@",NSStringFromSelector(_cmd));//_cmd是iOS内置的参数可以直接打印当前的方法名
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.
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.
NSLog(@"%@",NSStringFromSelector(_cmd));
} - (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.
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));
} @end
1.首次运行打印出来的消息是:
2016-06-30 17:17:19.610 测试状态改变[53450:338456] application:didFinishLaunchingWithOptions:
2016-06-30 17:17:19.613 测试状态改变[53450:338456] applicationDidBecomeActive:
2.程序切换到后台时,打印出来的消息是:
2016-06-30 17:17:24.516 测试状态改变[53450:338456] applicationWillResignActive:
2016-06-30 17:17:25.096 测试状态改变[53450:338456] applicationDidEnterBackground:
3.程序从后台切换到前台时,打印出来的消息是:
2016-06-30 17:19:18.335 测试状态改变[53450:338456] applicationWillEnterForeground:
2016-06-30 17:19:18.849 测试状态改变[53450:338456] applicationDidBecomeActive:
4.程序终止时,并不会执行 applicationWillTerminate:这个方法,而是直接报错。