1

我正在使用 Cordova 2.1.0 进行 IOS 应用程序开发。因为,我是应用程序开发的新手,所以我有一个非常基本的问题。

applicationDidEnterBackground当应用程序进入后台时,我正在使用方法来处理应用程序控制。但我想了解UIApplicationDidEnterBackgroundNotification应用程序进入后台时发送的实用程序。我可以通过什么方式使用UIApplicationWillEnterForegroundNotification系统发送的此通知和其他通知(如等)。这些通知的 USP 是什么。

4

1 回答 1

1

根据文档,该方法applicationDidEnterBackground:告诉UIApplication' 代表应用程序现在在后台。在 Cocoa 中,许多委托消息都有相应UINotification的 s 也被发送。这也不例外。

根据文档

应用程序还会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];

                     }];
于 2012-10-08T10:28:28.113 回答