how to cancel a localNotification with the press of a button in swift?

I am scheduling a location based UILocalNotification with the click of a button . But when i try to cancel the localNotification by clicking the same button again, it doesn’t cancel the notification.
I am using UIApplication.sharedApplication().cancelLocalNotification(localNotification)
to cancel my scheduled location based local notification.
What am i doing wrong ?
here is my implementation

@IBAction func setNotification(sender: UIButton!) {
    if sender.tag == 999 {
        sender.setImage(UIImage(named: "NotificationFilled")!, forState: .Normal)
        sender.tag = 0
        regionMonitor() //function where notification get scheduled

    } else {
        sender.setImage(UIImage(named: "Notification")!, forState: .Normal)
        sender.tag = 999 }

what should i enter into the else block so that the scheduled notification gets canceled. Cannot clear all notifications.
here is the didEnterRegion block code where i trigger the local notification

  • Scheduled Local Notifications showing up when toggled ON for time period when toggled OFF
  • scheduled local notification is not being stored in the scheduledLocalNotification array
  • Check for launching from UILocalNotification in Swift
  • UILocalNotification is deprecated in iOS10
  • UIApplication scheduledLocalNotifications is empty for past non-repeating notifications
  • Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift)
  • func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
        localNotification.regionTriggersOnce = true
        localNotification.alertBody = "Stack Overflow is great"
        NSLog("Entering region")

    5 Solutions Collect From Internet About “how to cancel a localNotification with the press of a button in swift?”

    You could try to remove all notifications if this is acceptable in your context.
    Like this:

    for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] { 

    Or as stated by Logan:


    Or as stated by Gerard Grundy for Swift 4:


    You can save a unique value for key in your local notification’s userinfo and cancel it by getting the localnotification using that key’s Value.
    Try this (for Objective C):

    NSArray *notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for (int i=0; i<[notifArray count]; i++)
        UILocalNotification* notif = [notifArray objectAtIndex:i];
        NSDictionary *userInfoDict = notif.userInfo;
        NSString *uniqueKeyVal=[NSString stringWithFormat:@"%@",[userInfoDict valueForKey:@"UniqueKey"]];
        if ([uniqueKeyVal isEqualToString:keyValToDelete])
            [[UIApplication sharedApplication] cancelLocalNotification:notif];

    Solution for iOS 10+ Swift 3.1

    let center = UNUserNotificationCenter.current()
    center.removeAllDeliveredNotifications() // To remove all delivered notifications

    For Swift 3.0 and iOS 10:


    Hard to tell without seeing the implementation of the code.

    so you have an

    - IBAction methodName{

    Is that about right? If so add a bool so

    Bool didCancel;
    - IBAction methodName{
    if didCancel == No{
    didCancel = YES;
    }else if didCancel == YES{
    didCancel = NO;