object-c AppDelegate代理函数生命周期详解

来自:千锋-iOS开发视频教程-UI第1季-1.8-UIApplication生命周期.mp4
一、AppDelegate代理函数
AppDelegate完成启动代理函数
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions

进入后台状态
- ( void )applicationWillResignActive:( UIApplication *)application
- ( void )applicationDidEnterBackground:( UIApplication *)application

进入前台状态
- ( void )applicationWillEnterForeground:( UIApplication *)application
- ( void )applicationDidBecomeActive:( UIApplication *)application

终止调用(>IOS4.0 plist  UIApplicationExistOnSuspend(就是 Application does not run in background ))
- ( void )applicationWillTerminate:( UIApplication *)application


收到内存警告
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application


二、代码解析

#import "HelloWorldAppDelegate.h"

@implementation HelloWorldAppDelegate

// AppDelegate第一个进入系统的函数,应用程序完成初始化回调函数,也是代理函数,也是入口函数
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 应用程序启动后覆盖定制服务。
    // Override point for customization after application launch.
    return YES;
}
// 即将进入后台
- (void)applicationWillResignActive:(UIApplication *)application
{
    // 应用程序即将从活跃状态切换到不活跃状态这个函数(消息)就会被调用。这也会在某些临时状态发生(比如来了电话,SMS短信,这个函数也会被调用)或者 当用户退出了应用程序,它开始切换到后台模式
    // 我们应该做什么?用这个方法我们要做暂停正常运行的任务。关闭定时器,降低OpenGL ES的分辨率。暂停游戏。
    // 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.
    //从应用程序到前台(按Home后)或者锁屏等等
    NSLog(@"function %@ is calling",NSStringFromSelector(_cmd));
}
// 已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 用这个方法来释放共享资源,保存用户数据,作废定时器,保存足够多用户状态信息以便程序被终止,我们下次还能恢复之前的状态信息
    // 如果你的应用程序支持后台调用,当用户退出应用程序(按Home键等)时这个方法就会被调用而不调用applicationWillTerminate消息
    // 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(@"function %@ is calling",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(@"function %@ is calling",NSStringFromSelector(_cmd));
}
//前台已经进入活跃状态(前台)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // 重新启动被暂停的任务。如果应用程序在后台,那么这里要刷新用户界面(UI)
    // 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(@"function %@ is calling",NSStringFromSelector(_cmd));
}
//终止调用函数(ios4.0不支持多任务,按Home键会调用这个函数杀死应用程序;ios4.0后按Home键将挂起应用程序这个函数就不会调用,若想调用就必须在plist给Application does not run in background键赋值TRUE)
- (void)applicationWillTerminate:(UIApplication *)application
{
    // 当应用程序停止时会调用这个函数,在适当情况下保存数据。这个函数只有在<ios4.0时调用或者ios>4.0设置了不能后台模式时
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"function %@ is calling",NSStringFromSelector(_cmd));
}
//收到内存警告,应用程序回调函数
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"function %@ is calling",NSStringFromSelector(_cmd));
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值