2012-10-08 105 views
1

我正在使用Cordova 2.1.0進行IOS應用程序開發。因爲,我對應用程序開發並不熟悉,所以我有一個非常基本的問題。通知實用程序'UIApplicationDidEnterBackgroundNotification'

我使用applicationDidEnterBackground方法來處理app進入後臺時的應用程序控制。但我想了解當應用程序進入後臺時發送的UIApplicationDidEnterBackgroundNotification的效用。我可以通過哪種方式使用此通知和系統發送的其他通知(如UIApplicationWillEnterForegroundNotification等)。這些通知的USP是什麼?

回答

1

根據文檔,方法applicationDidEnterBackground:告訴UIApplication的代表,該應用程序現在在後臺。在Cocoa中,許多代表消息都有相應的UINotification,這些消息也被髮送。這也不例外。

按照documentation

該應用程序還張貼在其周圍會調用這個方法讓感興趣的對象有機會的過渡響應的同時UIApplicationDidEnterBackgroundNotification通知。

因此,如果對象圖中有對象需要響應狀態轉換,他們可以觀察此通知。我不確定除了允許圖中的所有對象響應應用程序狀態轉換之外,還有一個沒有說明的目的。我想,如果你有一個長期運行的任務,當應用程序轉換到後臺任務時,你可以使用beginBackgroundTaskWithExpirationHandler:,其方式類似於你在applicationDidEnterBackground中做的操作。

編輯:

// example, save NSArray *_myArray to disk when app enters background 
// this is contrived, and untested, just meant to show how you can 
// observe the UIApplicationDidEnterBackgroundNotification and save state 
// in an arbitrary point in the object graph. (as opposed, or in addition to, the 
// application's delegate. 

// long-running tasks, e.g. web service connections, etc. will need to 
// get a background task identifier from the UIApplication and manage that. 

__block id enteredBackground = nil; 
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
enteredBackground = [center addObserverForName:UIApplicationDidEnterBackgroundNotification 
             object:nil 
             queue:nil 
            usingBlock:^(NSNotification *note) { 
             [_myArray writeToFile:@"/path/to/you/file" atomically:YES]; 

        }]; 
+0

感謝僅供參考。如果你可以給我一個例子,我怎麼可以在應用程序中使用這個通知。這將是非常有益的。謝謝。 – clint

相關問題