无论是学习object C语言还是其他语言,我们首先要了解的就是该语言在程序中是如何运行的,生命周期是怎样的。学习iOS手机开发,就得了解iOS程序的运行的生命周期是如何的,现在我们来了解下iOS程序的生命周期iOS.
iOS程序的入口程序和其他语言也是一样的,都是从mian函数还是启动,如下图main.m文件就是程序的启动入口
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
@autoreleasepool{} 这个是一个自动释放池,程序结束后会将内存释放
1.argc和argv参数是为了与C语言保持一致,在这没用到,不详述。
2.后面两个参数为principalClassName(主要类名)和delegateClassName(委托类名)。
UIApplicationMain函数,前两个和main函数一样,重点是后两个,官方说明解释是,后两个参数分别表示程序的主要类(principal class)和代理类(delegate class).
(1)如果principalClassName是nil,那么它的值将从Info.plist中获取,如果Info.plist中没有,则默认为UIApplication。principalClass这个类除了管理整个程序的生命周期之外什么都不做,它只负责监听事件然后交给delegateClass去做。
(2)delegateClass将在工程新建时实例化一个对象NSStringFromClass([AppDelegate class]) //相当于@"AppDelegate"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"didFinishLaunchingWithOptions");
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(@"applicationWillResignActive");
}
- (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(@"applicationDidEnterBackground");
}
- (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(@"applicationWillEnterForeground");
}
- (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(@"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"applicationWillTerminate");
}
1、application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次。若用户直接启动,lauchOptions内无数据,若通过其他方式启动应用,lauchOptions包含对应方式的内容。
2、applicationWillResignActive:在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。
3、applicationDidEnterBackground:在应用程序已进入后台程序时,要执行的委托调用。
4、applicationWillEnterForeground:在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与applicationWillResignActive 方法相对应。
5、applicationDidBecomeActive:在应用程序已被激活后,要执行的委托调用,刚好与applicationDidEnterBackground 方法相对应。
6、applicationWillTerminate:在应用程序要完全推出的时候,要执行的委托调用,这个需要要设置UIApplicationExitsOnSuspend的键值
下面给出打印就明白他们之间的交互先后顺序了:
启动程序
2017-05-26 15:20:55.617 05-Runtime(字典转模型)[96388:5466613] didFinishLaunchingWithOptions
2017-05-26 15:20:56.046 05-Runtime(字典转模型)[96388:5466613] applicationDidBecomeActive
按下home键回到后台
2017-05-26 15:21:10.642 05-Runtime(字典转模型)[96388:5466613] applicationWillResignActive
2017-05-26 15:21:11.366 05-Runtime(字典转模型)[96388:5466613] applicationDidEnterBackground
重新点击进入程序时
2017-05-26 15:21:15.462 05-Runtime(字典转模型)[96388:5466613] applicationWillEnterForeground
2017-05-26 15:21:15.910 05-Runtime(字典转模型)[96388:5466613] applicationDidBecomeActive