詳解IOS多任務機制 初學者必看文檔
IOS多任務機制是本文要介紹的內容,對IOS程序持久化很感興趣,于是首先研究了下iOS的多任務機制.對于大多數的應用,如果不是特別需要,用***的SDK編譯出來的程序本身就是支持多任務的—按home鍵程序進入后臺運行(但是注意此時的程序并不是會運行,只是進入后臺狀態便于其再次進入活動狀態,這一點同我們概念中應該有的多任務有區別).而對于一些應用,是需充分運用iOS多任務的特性,如游戲應用和需要網絡連接的應用等等。
多任務機制是蘋果在iOS 4中引進的,我們首先新建一個工程,查看下appdelegate文件中的內容:
- App cycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- { /
- / Override point for customization after application launch.
- // Set the view controller as the window's root view controller and display. 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.
- */
- }
- - (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, called instead of applicationWillTerminate: when the user quits.
- */
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- /*
- Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
- */
- }
- - (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.
- */
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- /* Called when the application is about to terminate.
- See also applicationDidEnterBackground:.
- */
- }
以上的函數就是ios4中引入的用于支持多任務運行的函數.從函數名我們就可以猜測到程序可能的一些狀態:background,active. 圖1就是程序的生命周期圖.
圖1
我們來看一下:程序***次啟動,從Not Running狀態進入active階段,程序會調用兩個函數:
- didFinishLaunchingWithOptions:和
- applicationDidBecomeActive:
此時點擊home鍵,程序就會進入后臺直至進入掛起狀態,程序會依次調用下述函數:
- applicationWillResignActive:
- applicationDidEnterBackground:
這時雙擊home鍵再進入該程序時,程序會依次調用:
- applicationWillEnterForeground:
- applicationDidBecomeActive:
注意到Background包含running和suspend兩種狀態.這里的running不是真正意義上的程序運行,而是指的applicationDidEnterBackground:運行部分,通常情況下,系統給此函數執行的時間不會太多,并且執行完后程序就在后臺掛起.這就是iOS多任務的絕大部分情形,但是等一下,就三種情況蘋果是允許你在后臺運行的
音樂
位置
VoIP
你可以在程序中的plist文件中進行設置,如圖2:
圖2
可以這么說,除了這三種服務允許以我們通常認為的多任務機制在后臺運行,其他的程序的多任務,就是上文所指的程序狀態.這里不討論為啥蘋果要這樣做,只是稍微說明一下,在這種情況下,至少是在表面上實現了多任務:比如說我玩了憤怒的小鳥又想看下天氣,然后又想玩小鳥了,這時切換就比較容易,因為程序這時是在后臺掛起,還是在內存中運行著的,這樣再啟動時就會比較快.
程序調用相應函數的時候,系統會發送相應的Notification,這時app就應該適時的保存app狀態或是讀取app的歷史狀態,這樣才能更好的呈現用戶體驗,之前說過,程序進入后臺的時間很短,有時候app要做的操作還沒來得及進行.這時,我們可以使用beginBackgroundTaskWithExpirationHandler:來處理耗時可能比較長的操作.
iOS所謂的多任務并不是我們通常以為的多任務,IOS只允許三種服務在后臺運行;其他的只是方便多個app之間的切換.,至于如何實現多任務,比如進入后臺程序應該優先進行哪些操作的細節,會在以后結合本人的實際加以說明。
小結:詳解iOS多任務機制 初學者必看文檔的內容介紹完了,希望本文讀你有所幫助!