Blip
Blip

Reputation: 1188

Why can't I cancel a repeating UILocalNotification?

Ciao, tout le monde. In my Swift app, there is a UILocalNotification scheduled in a method that will be called after some time when the UIViewController is loaded, and it is repeating every hour (using repeatInterval = .CalendarUnitHour). I store this UILocalNotification instance in a private property in the UIViewController so that I can cancel it later on. When the user presses a button in the UIViewController, the action method will trigger the cancellation of the notification instance, by which I use this code (the code is also used in the deinit of the UIViewController:

if let notification = notificationProperty {
    UIApplication.sharedApplication().cancelLocalNotification(notification)
    notificationProperty = nil
}

I use a if-let statement here to prevent accidentally canceling a nil UILocalNotification. But even after it canceled the notification, the notification is still showing up every hour.

So why is it not cancelling properly? Thanks!

Upvotes: 1

Views: 178

Answers (1)

Yedidya Reiss
Yedidya Reiss

Reputation: 5326

The notification that the system have is a copy of the one you are holding as a field. You can't cancel the one that you are holding since it is not a notification instance that is in the system.

You can cancel all the existing notifications by cancelAllLocalNotifications or find the one you want to cancel by iterating over all the existing instances by the scheduledLocalNotifications property.

Upvotes: 2

Related Questions