Search code examples
iosobjective-ciphoneuilocalnotificationnsnotifications

How to clear all scheduled local notifications when deleting the app


I am developing an iPhone app where the user can schedule local notifications as reminders.

In the applicationDidBecomeActive method, I am checking the expire dates of each scheduled notification and cancel the notifications which are expired.

Now my question is, if I delete the application from the phone and re-install, all the scheduled notifications (which are not expire yet) are still there.

BUT, my requirement is to cancel all the scheduled local notifications if the user delete the application for the phone.

How can I do it with

[[UIApplication sharedApplication] cancelAllLocalNotifications]

Solution

  • NSUserDefaults are deleted if you delete the app - so you could set an NSUserDefault with some value, and check it when the app starts up.

    If the value is nil, you know it's either a fresh install or that the app has been deleted. Then you can call to cancel the local notifications.

    After canceling the existing notifications (if it's a fresh install, nothing will happen anyway since there will be no notifications), you can set the NSUserDefault again, so that next time the cancel operation won't be called.