0

There is a small bug in my App. My App displays notifications at specific times when the App is running and cancel all of them whenever a button is switched. My problem is that whenever a user closes the App using the multitasking feature of iOS the notifications are still showing up.

I tried to add the following code which doesn't work:

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

The problem is that my App should show notifications when the App is running but shouldn't show notifications when the App is terminated. Why does the above code not work?

David H
  • 40,852
  • 12
  • 92
  • 138
  • Have you looked at the documentation when the app-lifetime methods are called? And which states an app can reach? – Mats Oct 27 '12 at 09:56
  • Not really but applicationWillTerminate indeed does link to something else, the problem is I want my notifications when the app IS running in the background but not when IT IS closed. – user1771336 Oct 27 '12 at 10:10
  • I have the same problem will update this report if I can find a solution. – David H Oct 27 '12 at 13:35
  • `applicationWillTerminate:` won't necessarily be called. If the app is currently in the background when the app is terminated, it actually WON'T be called at all. See here: http://stackoverflow.com/questions/7818045/applicationwillterminate-when-is-it-called-and-when-not – Kitsune Oct 27 '12 at 15:16

2 Answers2

0

Just because your app is visible in the app-changer, it doesn't mean it is still running.. it can get closed at any point. You cannot differentiate between the OS closing your app or the user closing your app.

Perhaps a button would be the solution? A button that cancels all notifications? Or you run a real background task (which can last for about 5 minutes) and stop all notifications afterwards. Or you just schedule the notifications for the next 5-10 minutes and that's it.

For what are you using them?

calimarkus
  • 9,955
  • 2
  • 28
  • 48
  • I do have a button added (Toggle on/off button) but some users told me this bug exists. So that's why I was wondering if there was a solution for this. But it seems this is not possible. – user1771336 Oct 27 '12 at 19:14
0

The correct answer is that this cannot currently be done by a multitasking app. One solution is to set a flag in our info.plist declaring your app wants to be killed when the user switches to another app - then you will get the willTerminate message (but get killed then).

There are huge numbers of threads on this topic, one which quotes an Apple doc that tells you backgrounded apps that are terminated do NOT get the willTerminate message is here.

For me, this just means I can now close an open bugreport out with a 'cannot fix' resolution :-)

Community
  • 1
  • 1
David H
  • 40,852
  • 12
  • 92
  • 138