iPhone - How to catch if the app is terminated (to update the badge icon)

人走茶凉 提交于 2019-12-25 03:35:00

问题


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, ...) ?


回答1:


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.




回答2:


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.




回答3:


Please go over the app lifecycle documentation as below

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

applicationWillResignActive:
applicationDidEnterBackground:


来源:https://stackoverflow.com/questions/9070358/iphone-how-to-catch-if-the-app-is-terminated-to-update-the-badge-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!