gpt4 book ai didi

objective-c - 通知实用程序 'UIApplicationDidEnterBackgroundNotification'

转载 作者:行者123 更新时间:2023-11-29 13:27:22 25 4
gpt4 key购买 nike

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

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

最佳答案

根据文档,applicationDidEnterBackground: 方法告诉 UIApplication 的委托(delegate)应用程序现在处于后台。在 Cocoa 中,许多委托(delegate)消息都有相应的 UINotification 也被发送。这也不异常(exception)。

根据documentation :

The application also posts a UIApplicationDidEnterBackgroundNotification notification around the same time it calls this method to give interested objects a chance to respond to the transition.

因此,如果你的对象图中有对象需要响应状态转换,它们可以观察到这个通知。除了允许图中的所有对象响应应用程序状态转换之外,我不确定是否真的有未说明的目的。我想如果你有一个长时间运行的任务要在应用程序转换到后台任务时执行对象层次结构中的某处,你可以使用 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];

}];

关于objective-c - 通知实用程序 'UIApplicationDidEnterBackgroundNotification',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12779649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com