// 程序启动完成后调用(常用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
return YES;
}
// 当app失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
// 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.
}
// app进入后台的时候调用(常用)
// app忽然被打断的时候,在这里保存一些需要用到的数据
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
// 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.
}
// app即将进入前台的时候调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
// 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.
}
// 当app获取到焦点的时候调用,意味着app可以和用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
// 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.
}
// app被关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// app接受内存警告的时候被调用(常用)
// 清空图片缓存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"%s__%d", __FUNCTION__, __LINE__);
}
注意:
application:didFinishLaunchingWithOptions:: App 首次启动时调用,一般在这个函数里创建 window 对象,初始化 App 可能用到的第三方 SDK 等。
applicationWillResignActive::App 将要进入后台时调用,比如有电话进来或者按下 Home 键。
该函数里面主要执行操作:
- 暂停正在执行的任务;
- 停止计时器;
- 减少OpenGL ES帧率。
applicationDidEnterBackground::App 已经进入后台,一般该方法用来:
- 释放共享资源;
- 保存用户数据(写到硬盘);
- 销毁计时器。
applicationWillEnterForeground::App 即将进入前台,一般该方法用来撤销 applicationWillResignActive: 中做的改变。
applicationDidBecomeActive::App 已经进入前台,若 App 之前在后台,在此方法内刷新用户界面。