Cancel UILocalNotification

My solution is to use the UILocalNotification userInfo dictionary. In fact what I do is to generate a unique ID for each of my notifications (of course this ID is something I'm able to retrieve later), then when I want to cancel the notification associated to a given ID I will simply scan all available notifications using the array:

[[UIApplication sharedApplication] scheduledLocalNotifications]

and then I try to match the notifications by investigating the ID. E.g.:


NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
  if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
     notificationToCancel=aNotif;
     break;
  }
}
if(notificationToCancel) [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

I don't know if this approach is better or not with respect to the Archiving/Unarchving one, however it works and limits data to be saved to just an ID.

Edit: there was a missing braket


My solution is to archive UILocalNotification object you have scheduled with NSKeyedArchiver and store it somewhere (in a plist is preferred). And then, when you want to to can cancel the notification, look up the plist for the correct data and use NSKeyedUnarchiver to unarchive. The code is pretty easy:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice];

and

UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];

My solution is you when you create UILocalNotification at that time you create one NSMutableDictionary and store that notification as a value for key as your ID and put this NSMutableDictionay to your NSUserDefaults

So when you want to cancel any particular localnotification at that time you write [dictionary valueforkey @"KEY"] where as a key you pass your id so you get that particular local notification and pass it to

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

You can get a list of all scheduled notifications from scheduledLocalNotifications or you can cancel them all:

  [[UIApplication sharedApplication] cancelAllLocalNotifications];