標籤:
為了研究應用的生命週期,在每個方法裡面加入列印當前的函數名的方法:
如下:
1、運行程式:
輸出:
2、按一下home鍵
3、再點擊應用
4、雙擊
Home鍵,向上滑動應用,殺掉應用
這個時候控制台沒有任何輸出,因為你直接殺死應用是不會調用applicationWillTerminate的,只有當應用被系統殺死的時候才會調用這個方法。
5、補充
當收到推播通知的時候會得到以下輸出:applicationWillResignActive:
在彈出的通知橫幅處向上滑動,讓橫幅消失,這時會得到以下輸出:applicationDidBecomeActive
總結:
// 應用程式的生命週期方法。// 應用程式完成啟動以後調用的方法。// 在這個方法中我們可以建立要顯示的控制項。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 列印當前的函數名,以及當前代碼所在檔案中得行數// 列印當前的函數名, NSStringFromSelector 獲得參數的選取器所代表的方法的字串 NSLog(@"%@", NSStringFromSelector(_cmd)); // 在這兒加入要顯示的控制項 // 在應用程式上要顯示控制項,必須要有視窗,控制項的顯示都是直接或者間接放到視窗上進行顯示, 一般情況下,一個應用程式只有一個視窗。 // 建立視窗對象 self.window = [[UIWindow alloc] init]; // UIScreen 螢幕類 mainScreen取到當前裝置的主畫面 ,bounds取到螢幕的大小 // 設定視窗的大小和座標 self.window.frame = [[UIScreen mainScreen] bounds]; // 設定視窗的背景顏色為紅色 self.window.backgroundColor = [UIColor redColor]; // 讓視窗顯示 [self.window makeKeyAndVisible]; return YES;}//後台切換到前台: 後台 --> 不活躍狀態 --> 前台(活躍狀態)//前台切換到後台: 前台(活躍狀態) --> 不活躍狀態 --> 後台- (void)applicationWillResignActive:(UIApplication *)application{ NSLog(@"%@", NSStringFromSelector(_cmd)); // 方法何時被調用? // 這個方法在應用程式即將從活躍狀態切換到不活躍狀態時調用,這種情況會在某些臨時中斷的事情下發生(比如來電話,來簡訊等)或者使用者退出應用程式時發生。並且應用程式開始轉換到後台狀態。 // 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. // 在這個方法中,我們可以做啥? // 使用這個方法,我們可以暫停正在執行的任務,停用定時器,降低OpenGL ES的描繪幀率,遊戲應該在這個方法中暫停。 // 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.}// 應用程式進入到後台後調用的方法。- (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. // 配置應用程式是否能夠在後台: 在Info.plist添加一項:Application does not run in background YES 不支援後台運行 NO 支援後台運行 // 當使用者退出應用程式時,如果你的應用程式支援後台運行,這個方法會被調用.如果你的應用程式不支援後台運行,applicationWillTerminate:這個方法會被調用。 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. NSLog(@"%@", NSStringFromSelector(_cmd));}// 應用程式即將進入到前台時調用的方法- (void)applicationWillEnterForeground:(UIApplication *)application { // 這個方法作為從後台進入不活躍狀態的時候調用的方法。在這兒,你可以撤銷在進入後台後做的許多改變。(恢複使用者資料) NSLog(@"%@", NSStringFromSelector(_cmd));}// 應用程式已經成為活躍狀態後調用的方法- (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(@"%@", NSStringFromSelector(_cmd));}- (void)applicationWillTerminate:(UIApplication *)application { // 這個方法在應用程式即將終止時調用。在這個方法中儲存合適的資料。 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"%@", NSStringFromSelector(_cmd));}// 應用程式收到記憶體警告時調用的方法- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{ // 釋放暫時無用的資源 NSLog(@"%@", NSStringFromSelector(_cmd));}
iOS 應用的生命週期