什么是UIApplication
UIApplication继承自UIResponder,的主要作用是提供了iOS程序运行期间的控制和协作工作,每个iOS程序运行期间都会有一个UIApplication实例,一般由main函数的UIApplicationMain函数完成,而在代码中可以调用[UIApplication sharedApplication]方法来得到这个实例(有且仅有一个)。
UIApplication监听系统事件
app启动时调用,程序加载完毕
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
程序被唤醒,激活(程序获取焦点)
- (void)applicationDidBecomeActive:(UIApplication *)application;
注意:应用程序在启动时,在调用了 applicationDidFinishLaunching 方法之后也会调用 applicationDidBecomeActive 方法,所以你要确保你的代码能够分清复原与启动,避免出现逻辑上的bug。
程序挂起(程序失去焦点,比如当有电话进来或者锁屏时,会调用这个方法)
- (void)applicationWillResignActive:(UIApplication *)application;
程序进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application ;
程序从后台进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application ;
程序内存警告,可能要终止程序
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
程序即将退出
- (void)applicationWillTerminate:(UIApplication *)application ;
首次运行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationDidBecomeActive:(UIApplication *)application
首次关闭(home):
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
再次运行:
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
再次关闭:
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application