0

i have in my app about 10 local notification (not in array )and its in switch. I used this code

    NSUserDefaults *defaultsB = [NSUserDefaults standardUserDefaults];
    UILocalNotification *notification = [[UILocalNotification alloc]init];

    if (switcher.on == 1) {

        NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSWeekCalendarUnit fromDate:[NSDate date]];

        [dateComponent setWeekday:1]; // For sunday
        [dateComponent setHour:[timeHClass.text integerValue]];
        [dateComponent setMinute:[timeMClass.text integerValue]];

        NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];


        [notification setAlertBody:textClass1.text];
        [notification setFireDate:fireDate];
        notification.soundName = @"bells.mp3";
        notification.repeatInterval = NSWeekCalendarUnit;
        [notification setTimeZone:[NSTimeZone defaultTimeZone]];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];


        [defaultsB setObject:@"ON" forKey:@"SwitchState"];
    }
    else  {

        UIApplication *app=[UIApplication sharedApplication];
        [app cancelLocalNotification:notification];



        [defaultsB setObject:@"OFF" forKey:@"SwitchState"];

    }

but when i turned switch off the notification still working ??

should i save the notification save in NSUserDefaults not just the switch ??

and if its yes how ??

Faisal
  • 17
  • 6

1 Answers1

2

If you want to cancel an existing notification you need to get a reference to it via [[UIApplication sharedApplication] scheduledLocalNotifications] then loop through those notifications and find the one you want (presumably using a unique identifier you gave it during creation).

Alternatively, if you want to cancel all notifications replace:

[app cancelLocalNotification:notification];

With:

[app cancelAllLocalNotifications];
thattyson
  • 718
  • 8
  • 17
  • so you are saying i should put in array to get the cancellation work ? but way my way didn't work ? and how can i put it in array if every notification in single switch ?... I'm beginner – Faisal Jul 13 '15 at 13:24
  • @Faisal if all you're doing is creating one local notification in your app, simply cancel all notifications with `[app cancelAllLocalNotifications];`. If you need to cancel a specific notification I'd suggest following [this answer](http://stackoverflow.com/questions/7186236/cancelling-a-specific-uilocalnotification). – thattyson Jul 13 '15 at 15:55