iPhone - How to catch if the app is terminated (to

2019-08-18 15:22发布

问题:

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: