2

I am trying to figure out how to fire a local notification on specific days.

I have to use the calendar units, but the explanations I found about them are a stinking piece. Can you guys please explain to me, as if I were five, what intervals they represent?

I have filled the ones I think I know, please correct me if I am wrong and fill the others.

NSEraCalendarUnit = ????? era? 
NSYearCalendarUnit = event will repeat once in a year at the same month, day and hour
NSMonthCalendarUnit = event will repeat once a month at the same day and hour
NSDayCalendarUnit = event will repeat every day at the same hour
NSHourCalendarUnit = event will repeat every hour at the same minute
NSMinuteCalendarUnit = event will repeat every minute at the same second
NSSecondCalendarUnit = event will repeat every second
NSWeekCalendarUnit = event will repeat the same day of week every week 
NSWeekdayCalendarUnit = ?????? can I make it repeat just specific days? I mean, monday to friday but not saturday and sunday?
NSWeekdayOrdinalCalendarUnit = ????? how do I use that?
NSQuarterCalendarUnit = when exactly will it repeat? every 3 months?
NSCalendarCalendarUnit = ?????????
NSTimeZoneCalendarUnit = ????????

this is an example of what I need to do:

UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
    alarm.fireDate = aDate;
    alarm.timeZone = [NSTimeZone systemTimeZone];
    alarm.repeatInterval = WHAT DO I PUT HERE? TO MAKE IT REPEAT JUST SATURDAYS AND SUNDAYS?;
    alarm.soundName = @"alarm.caf";
    alarm.alertBody = @"hi there";
    [app scheduleLocalNotification:alarm];
}

I need to program the local notifications in one of these possibilities:

  • to fire every day from monday to friday, except saturday and sunday
  • to fire every saturday and sunday and no other day
  • to repeat every week at the same day of week

what values should I use for the repeatInterval to accomplish this?

thanks

Mike Hay
  • 2,828
  • 21
  • 26
Duck
  • 34,902
  • 47
  • 248
  • 470

3 Answers3

2

I believe what you want can't be done. This question may give more details.

Of course, what you can do is make two separate local notifications, both set to repeat weekly, one that fires on Saturdays and one that fires on Sundays.

Community
  • 1
  • 1
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
  • Good idea, thanks. Can you please explain me what do those entries with question marks on my question? what the hell is NSWeekdayCalendarUnit? what about NSCalendarCalendarUnit? Apple should fire these guys writing their despicable docs. Their docs are the inverse of their products, quality speaking. – Duck Sep 16 '11 at 15:29
0

Q:to fire every day from monday to friday, except saturday and sunday

A:just get five UILocalNotification instances and set thire firedate from Monday to Friday(the firedate's year ,month,day is free so long as the weekday is what you want).then set thire repeatInterval NSWeekdayCalendarUnit.after schedule them to UIApplication.

(ps:you can solve other questions use the same method) hope it work!

NYZ_Star
  • 1
  • 1
-1

Your are doing it the wrong way I believe.

Here is what I would say to a five year old (though if he can comprehend obj/c at 5 I'd hire him quickly).

What do you want to accomplish ? You want to fire an alarm or you want to run specific actions at specific days of the week ?

He would probably say: "i don't know" but here you actually want to run specific actions on saturday and sundays.

To do so, you indeed need a timer that will run for example every day, or every hour, or every minute it really doesn't matter, the timer will only trigger the action that checks whether or not the action will occur.

NSTimer* aTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] 
                                                   interval:3600*24 // every 24 hours for example 
                                                     target:self 
                                                   selector:@selector(onTimerTriggered:) 
                                                   userInfo:nil 
                                                    repeats:YES];

Now you need to actually check to see if which day of the week we are. The most important thing is that this is done separately.

- (void)onTimerTriggered:(id)sender
{
    // Create your calendar (gregorian is what you need)
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Create your components specified to only keep track of the week day of today
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

    // now you know which day we are !! (careful in the US, sunday is first day of the week)
    int weekday = [weekdayComponents weekday];

    // trigger alarm if needed
    if (weekday == 3 || weekday == 5) // wednesday or saturday
    { ... } // trigger your alarm instantly or wait until a specific hour it's up to you
}

So to sum up, decompose your problem into more specific actions. You want to action create specific actions on different days ?

  1. Check what day of the week it is
  2. Action !

The NS--CalendarUnit are only used to decompose a specific date into what Apple call a NSDateComponents but this is definitely Apple way of dealing with date decomposition and is not written in steel for other languages.

apouche
  • 9,703
  • 6
  • 40
  • 45
  • thanks, but my problem is this it is not possible to fire an alarm using timers for big intervals. Timers just work when the app is running. The problem is this. At some time the app will quit. I need then, before the app quits, to program iOS with UILocalNotifications, that is the only way I know the event can be fired in the future while the app quits running. If I don't do that, events will not be fired. The periodicities I need for the UILocalNotifications to repeat are thos I mentioned at the end on my question. What I don't see is how can I use your answer to do that. Thanks anyway. – Duck Sep 16 '11 at 07:55
  • indeed my apologies, i misunderstood your problem. now the correct solution lies beyond my knowledge. – apouche Sep 16 '11 at 08:35