根據文檔,方法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];
}];
感謝僅供參考。如果你可以給我一個例子,我怎麼可以在應用程序中使用這個通知。這將是非常有益的。謝謝。 – clint