Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

iphone - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To calculate the next fire date for a repeating UILocalNotification, you have to:

  1. Figure out the amount of repeatInterval there's been between the notification's original fire date (i.e. its fireDate property) and now.
  2. Add them to the notification's fireDate.

Here's one approach:

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *difference = [calendar components:notif.repeatInterval
                                           fromDate:notif.fireDate
                                             toDate:[NSDate date]
                                            options:0];

NSDate *nextFireDate = [calendar dateByAddingComponents:difference
                                                 toDate:notif.fireDate
                                                options:0];

This works in many scenarios, but here's a scenario where it won't work:

Suppose that:

  • the notification's `fireDate is 01/01 at 2:00pm
  • the notification's repeatInterval is NSDayCalendaryUnit (i.e. repeat daily)
  • The date now is 08/01 at 3:00pm

The above code will calculate the difference to 7 days (01/01 + 7 days = 08/01), add them to fireDate, and thus set nextFireDate to 08/01 at 2pm. But that's in the past, we want nextFireDate to be 09/01 at 2pm!

So if using the above code and your repeatInterval is NSDayCalendaryUnit, then add these lines:

if ([nextFireDate timeIntervalSinceDate:[NSDate date]] < 0) {
    //next fire date should be tomorrow!
    NSDateComponents *extraDay = [[NSDateComponents alloc] init];
    extraDay.day = 1;
    nextFireDate = [calendar dateByAddingComponents:extraDay toDate:nextFireDate options:0];
}

I marked this answer as community wiki, feel free to edit it if you have found a better way to do the calculation!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...