Question

I am New to NSCalander, NSdates, and NSDateComponents

Basically I have a Local Notification and I want to repeat the fire date based on the selection of the user, let's say on Sunday and Monday only.

We should use repeatCalendar property for the UILocalNotification but I couldn't reach how to setup it.

So any One can help me with simple line of codes ?

Thanks

Was it helpful?

Solution

There is a code snippet to set the UILocalNotification to fire at every sunday 20:00.

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit|  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]  init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;

NSLog(@"notification: %@",notification);

[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

enjoy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top