0

In my app, I set up local notifications to notify the user while the app is running. In order to clean these up in case the app is terminated, I have set up applicationWillTerminate: to clear these notifications:

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"applicationWillTerminate");
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

Unfortunately, if the app crashes, or I terminate debugging, this function isn't called and therefore I still get the local notifications delivered to the device.

Is there a way of definitely checking if the application has quit unexpectedly so I can tidy these up?

jowie
  • 8,028
  • 8
  • 55
  • 94

1 Answers1

0

I'm using this KSCrash library with great success. Maybe it can help you also:

https://github.com/kstenerud/KSCrash

Or alternatively, have you set UIApplicationExitsOnSuspend on your .plist file? If you don't, there is no guarantee that applicationWillTerminate will be called, when using iOS 4.

neowinston
  • 7,584
  • 10
  • 52
  • 83
  • It looks like a good crash reporter, but unfortunately it only allows a call to a C method on crash, not Objective-C, and therefore I won't be able to call `cancelAllLocalNotifications`. – jowie Nov 28 '12 at 10:46
  • OK. I'll see if I can find another solution. Thanks for your message. – neowinston Nov 28 '12 at 14:35
  • Based on your idea, I tried Crittercism, which has an Objective-C delegate for when it crashes... Unfortunately that doesn't work either. – jowie Nov 28 '12 at 17:33
  • I was thinking about Crittercism also. I've updated my answer and maybe that can help you somehow. Thanks! – neowinston Nov 28 '12 at 18:47
  • Unfortunately I cannot use `UIApplicationExitsOnSuspend` either, because my app needs to work in the background. The local notifications I set are supposed to remind you in the background, however if the app crashes or is unloaded, I need for the local notifications to be removed. – jowie Nov 29 '12 at 09:27
  • I've altered the name of my question so it's a bit more apparent what I'm trying to do :) – jowie Nov 29 '12 at 09:29
  • I have also found that this is a possible duplicated of http://stackoverflow.com/questions/5398191/cancelalllocalnotifications-in-applicationwillterminate - I'll let the moderators decide what to do with this question :) – jowie Nov 29 '12 at 09:33
  • Yes, it seems like a duplicate. Any progress so far? – neowinston Nov 29 '12 at 13:34
  • Not really. I considered removing them on memory warning, but this probably won't get fired if the OS decides to quit the app while it is in the background. I also considered only firing off local notifications instantly when needed (I can do this because the app runs location services in the background), but I fear it would require a lot of reprogramming! – jowie Nov 29 '12 at 15:33
  • As a note: whenever your app crashes you may only run async safe code, which by default excludes to run any Objective-C code. See http://landonf.bikemonkey.org/code/objc/Reliable_Crash_Reporting.20110912.html – Kerni Jan 20 '13 at 19:57