iPhone - 如何捕获应用程序是否终止(更新徽章图标)

发布于 2024-12-29 21:47:02 字数 296 浏览 4 评论 0原文

我正在设置:

- (void)applicationWillTerminate:(UIApplication *)application
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
}

在我的应用程序委托中,但徽章从未更新...可能是什么问题?

当应用程序被终止时(手机关闭、任务栏应用程序终止、应用程序因内存过载而被系统终止,...),我该如何更新该徽章?

I'm setting :

- (void)applicationWillTerminate:(UIApplication *)application
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
}

in my application delegate but the badge is never updated... What can be the problem ?

How may I do to update that badge when the app is killed (phone shutdown, taskbar app kill, app killed by the system because of a memory overload, ...) ?

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

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

发布评论

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

评论(3

萤火眠眠 2025-01-05 21:47:03

当您的应用程序在后台终止时,它没有机会自行清理。应用程序在后台时已暂停;当它被杀死时,它只是从 RAM 中删除。您的徽章更新需要在此时间之前进行。

来自 Apple 文档

已暂停
该应用程序在后台但不执行代码。系统会自动将应用程序移至此状态,并且在此之前不会通知它们。挂起时,应用程序保留在内存中,但不执行任何代码。
当出现内存不足的情况时,系统可能会在没有通知的情况下清除挂起的应用程序,为前台应用程序腾出更多空间。

还:

applicationWillTerminate:—让您知道您的应用程序正在被终止。如果您的应用程序被挂起,则不会调用此方法。

When your app is terminated in the background, it does not get an opportunity to clean up after itself. The app is already suspended when backgrounded; when killed it is just removed from RAM. Your badge updates need to happen before that time.

From the Apple docs:

Suspended
The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code.
When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

Also:

applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

鹿港巷口少年归 2025-01-05 21:47:03

NSLog(@"App is being returned."); 放入您的 -applicationWillTerminate: 方法中。我想您会发现当您按原样终止应用程序时不会调用此方法。

您可以在应用程序正常执行期间或启动应用程序时设置应用程序的徽章图标。

Throw an NSLog(@"App is being terminated."); into your -applicationWillTerminate: method. I think you'll find that this method isn't being called when you are terminating the app the way you are.

You could set the application's badge icon during the app's normal execution or when the app is launched.

念三年u 2025-01-05 21:47:03

请仔细阅读 应用程序生命周期文档如下

另请尝试查看正在调用以下哪些方法

applicationWillResignActive:
applicationDidEnterBackground:

Please go over the app lifecycle documentation as below

Also try and see which of the below methods are being called

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