Search code examples
iphonedatesetintervaluilocalnotification

How to grab the NEXT fire date from a UILocalNotification object


I have a UILocalNotification object that I have setup with repeat intervals day, week, and month. I am having no troubles at all accessing the fire date of the object:

[cell.detailTextLabel setText:[notification1.fireDate description]];

But I am having troubles getting the next fire date. If I print out the above notification1 object to the console, I get this:

<UIConcreteLocalNotification: 0x613e060>{fire date = 2010-11-29 03:53:52 GMT, time zone = America/Denver (MST) offset -25200, repeat interval = 16, next fire date = 2010-11-30 03:53:52 GMT}

This object contains somewhere the value or data I need to display the next fire date...but I can't find it! Does anybody know where I can get it programmatically?

Thanks


Solution

  • I don't think the next fire date is available as a property but rather calculated from fireDate and repeatInterval. Date calculating can be tricky with different time zones and other nasty things. In your example you have chosen a daily repeat and to calculate the next fire date you can do something along the lines of:

    NSCalendar *calendar = localNotif.repeatCalendar;
    if (!calendar) {
      calendar = [NSCalendar currentCalendar];
    }
    
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
    components.day = 1;
    NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:localnotif.fireDate options:0];
    

    If you use some other repeat interval you would have to change the code accordingly. If you were to use NSMonthCalendarUnit you would have to use components.month = 1 instead.