12

I want to make a UILocalNotification repeat every week, before iOS7 I would use localNotification.repeatInterval = NSWeekCalendarUnit - except NSWeekCalendarUnit has been deprecated.

The docs say:

"Use NSCalendarUnitWeekOfMonth or NSCalendarUnitWeekOfYear, depending on which you mean"

But I don't know what the difference is, so I don't know which one I mean.

daihovey
  • 3,485
  • 13
  • 66
  • 110

2 Answers2

26

As for the UILocalNotification

NSCalendarUnitWeekOfYear should be used as a unit for repeatInterval.

When I set repeatInterval = NSCalendarUnitWeekOfMonth for the notification scheduled 10 seconds from now, the notification doesn't fire, and the description of the instance of UILocalNotification prints .. next fire date = (null) ...

If I use NSCalendarUnitWeekOfYear then notification appears and I see correct next fire date from code in the notification's description.

zubko
  • 1,718
  • 25
  • 28
  • Odd, `NSCalendarUnitWeekOfMonth` worked for me for some time, but then at some point it got broken. `NSCalendarUnitWeekOfYear` fixed the bug. – kas-kad Dec 09 '15 at 17:17
  • 1
    Does ```NSCalendarUnitWeekOfYear``` repeat the notification every week? – Supertecnoboff Mar 29 '16 at 09:48
  • 1
    @Supertecnoboff `NSCalendarUnitWeekOfYear` is just a const. If you set `repeatInterval` of the `UILocalNotification` instance to this const and then schedule that notification, and considering all other needed setup for local notifications is successful, then the notification will be repeated once a week after its initial `fireDate`. – zubko Mar 29 '16 at 11:24
  • 1
    @zubko Awesome thanks. Yeah I knew it was a const (or enum), but looking on the developer documentation, all I could find was a 2 line description, which wasn't very helpful to be honest. – Supertecnoboff Mar 29 '16 at 11:26
9

These don't appear to be documented but most likely NSCalendarUnitWeekOfYear will be a value from 1-53 (or maybe 0-52) representing the week number within the calendar year while NSCalendarUnitWeekOfMonth will be 1-5 (or maybe 0-4) representing the week within the month.

This all assumes Gregorian calendar. I can't speak to other calendar types.

It seems that NSCalendarUnitWeekOfMonth is equal to kCFCalendarUnitWeekOfMonth which is defined as "Specifies the original week of a month calendar unit." and NSCalendarUnitWeekOfYear is equal to kCFCalendarUnitWeekOfYear which is defined as "Specifies the original week of the year calendar unit.".

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • But which actually just means a week? Both? Or do I just hack a 7 * NSDayCalendarUnit ? – daihovey Mar 12 '14 at 04:44
  • What data are you trying to get? In other words, how are you trying to use one of these values? – rmaddy Mar 12 '14 at 04:47
  • 3
    I'm trying to get the UILocalNotification to repeat every week. – daihovey Mar 12 '14 at 05:44
  • It would be best to update your question showing what you are trying to do. Include your relevant code showing where you need to make use of one of these constants. It's hard to help with no context. – rmaddy Mar 12 '14 at 05:48
  • Assuming you want to repeat every week of the year, I would try `NSCalendarUnitWeekOfYear`. – rmaddy Mar 12 '14 at 18:11
  • But how does that differ from `NSCalendarUnitWeekOfMonth` - will it still repeat every week of the year? – daihovey Mar 13 '14 at 04:42
  • I honestly don't know how these values apply to setting up a local notification. My answer applied to your original question, before you mentioned it was for `UILocalNotification`. Apple's docs appear to need to be updated in this regard. – rmaddy Mar 13 '14 at 04:46
  • 1
    OK thanks - I guess I just need to wait a week and a month and year to fully debug the notification ;) – daihovey Mar 13 '14 at 21:44
  • Wouldn't the usage of these two vary on the ordinality? – Sumit Gera Mar 28 '15 at 14:13