2

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

Apurv
  • 17,116
  • 8
  • 51
  • 67
Atef
  • 2,872
  • 1
  • 36
  • 32

1 Answers1

6

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

chancyWu
  • 14,073
  • 11
  • 62
  • 81
  • One thing and your answer will be the best ;) How to add multiple days ? say Sunday & Monday ? – Atef Jan 23 '14 at 07:43
  • Then you can set two notifications. one for every sunday, one for every monday. – chancyWu Jan 23 '14 at 07:44
  • These is the problem I want to fix, for sure Apple didn't make repeatCalendar property for making two notifications to the same event, let's say I want to make a 10 reminder and reminder me at 5 days of the week , then I should create 50 notifications ?! don't make scene I think there is a way to merge the days in calendar like masking or something else ! – Atef Jan 23 '14 at 07:48
  • I've searched more and I think your answer was right, I've to set a reminder for each day. http://stackoverflow.com/questions/5938528/uilocalnotification-repeatinterval-on-days-of-week – Atef Jan 23 '14 at 13:52