無論是學習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