iOS應用程序的生命週期

無論是學習object C語言還是其他語言,我們首先要了解的就是該語言在程序中是如何運行的,生命週期是怎樣的。學習iOS手機開發,就得了解iOS程序的運行的生命週期是如何的,現在我們來了解下iOS程序的生命週期iOS.

iOS程序的入口程序和其他語言也是一樣的,都是從mian函數還是啓動,如下圖main.m文件就是程序的啓動入口

main函數入口

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

此爲iOS程序的生命週期,初學者應簡單學習,加深基礎的瞭解
發佈了30 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章