How to grab the NEXT fire date from a UILocalNotification object

前端 未结 4 760
[愿得一人]
[愿得一人] 2020-12-06 08:13

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:

4条回答
  •  借酒劲吻你
    2020-12-06 08:54

    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.

提交回复
热议问题