I've spent half of my day reading all "How to cancel a local notification" questions and answers. After all, I came up with my own solution but apparently it is not working. I have a tableview with all my scheduled notifications....
on the H file I have
@property (strong, nonatomic) UILocalNotification *theNotification;
and then on the M file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
theNotification = [notificationArray objectAtIndex:indexPath.row];
NSLog(@"Notification to cancel: %@", [theNotification description]);
// NSLOG Perfectly describes the notification to be cancelled. But then It will give me "unrecognized selector"
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Local Reminder"
message:@"Cancel local reminder ?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alertView show];
[alertView release];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"Cancel");
}else{
NSLog(@"Ok");
[[UIApplication sharedApplication] cancelLocalNotification:theNotification];
}
}
If I click "Ok" I get: 2012-02-04 03:34:48.806 Third test[8921:207] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x890ae90 Program received signal "SIGABRT".
If I can totally identify the notification to be cancelled why does it give me that ?
I found a way that I can make it look a little better. If you want to delete the localNotification straight from the table, you can add a "cancel" or "delete" button to each cell. like so:
And then you code your button :
That's just another way to do it. Seems to me a little better to visualize.
In my app I did it like this:
And when I scheduled local notification, I added a key. FlightNo is a unique ID for notification.
Note from Nick Farina: this works for scheduled notifications only; you can't seem to cancel a notification presented via
presentLocalNotificationNow
: