UIApplicationDidEnterBackgroundNotification

发布于 2024-10-15 09:45:19 字数 92 浏览 0 评论 0原文

iPhone 应用程序中 UIApplicationDidEnterBackgroundNotification 有什么用,或者我们如何从中受益

whats the use of UIApplicationDidEnterBackgroundNotification in iPhone app or how we can take benifit from it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-10-22 09:45:19

此通知意味着用户在 iPhone 4 上“退出”您的应用程序 - 当接到电话或短信并且用户接受中断(应答/回复)时,或者当用户按下主页按钮时,就会发生这种情况。

我在 SO 上找到了这个链接,它显示了所有状态之间的交互以及相应的通知:
http://www.drobnik.com/touch/2010 /07/understanding-ios-4-backgrounding-and-delegate-messaging/

要利用此通知,您可以按照 @Antwan 建议实现 applicationDidEnterBackground (在您的 UIApplicationDelegate 类中 - 这是主类)。

或者,您可以在代码中的任意位置设置通知处理程序:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

祝您好运!

奥德.

This notification means the user "quit" your app on an iPhone 4 - It happens when a phone call or text message comes in and user accepts the interruption (answers/replies), or when the user has pressed the Home button.

I found this link on SO that shows the interaction between all states, and the appropriate notifications:
http://www.drobnik.com/touch/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

To make use of this notification you can implement applicationDidEnterBackground as @Antwan suggested (in your UIApplicationDelegate class - that's the main class).

Alternatively you could set up a notification handler wherever you want/need in your code:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

Good luck!

Oded.

薯片软お妹 2024-10-22 09:45:19

来自苹果 文档

告诉委托人应用程序现在处于后台。

- (void)applicationDidEnterBackground:(UIApplication *)application

参数
应用
单例应用程序实例。

讨论
在 iOS 4.0 及更高版本中,当用户退出支持后台执行的应用程序时,将调用此方法而不是 applicationWillTerminate: 方法。您应该使用此方法来释放共享资源,保存用户数据,使计时器无效,并存储足够的应用程序状态信息,以便将应用程序恢复到当前状态,以防应用程序稍后终止。您还应该禁用应用程序用户界面的更新,并避免使用某些类型的共享系统资源(例如用户的联系人数据库)。您还必须避免在后台使用 OpenGL ES。

此方法的实现大约有五秒钟的时间来执行任何任务并返回。如果您需要额外的时间来执行任何最终任务,您可以通过调用 beginBackgroundTaskWithExpirationHandler: 向系统请求额外的执行时间。实际上,您应该尽快从 applicationDidEnterBackground: 返回。如果该方法在时间耗尽之前没有返回,您的应用程序将被终止并从内存中清除。

您应该在此方法退出之前执行与调整用户界面相关的任何任务,但其他任务(例如保存状态)应根据需要移至并发调度队列或辅助线程。由于您在 applicationDidEnterBackground: 中启动的任何后台任务可能要等到该方法退出后才会运行,因此您应该在启动这些任务之前请求额外的后台执行时间。换句话说,首先调用 beginBackgroundTaskWithExpirationHandler:,然后在调度队列或辅助线程上运行任务。

应用程序还会在调用此方法的同时发布 UIApplicationDidEnterBackgroundNotification 通知,以便让感兴趣的对象有机会响应转换。

From apple documentation.

Tells the delegate that the application is now in the background.

- (void)applicationDidEnterBackground:(UIApplication *)application

Parameters
application
The singleton application instance.

Discussion
In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should 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. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your application is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文