0
votes

I am trying to find next fire date of UILocalNotification here is the code

@implementation UILocalNotification (MyNextFireDate)
- (NSDate *)myNextFireDateAfterDate:(NSDate *)afterDate
{
    // Check if fire date is in the future:
    if ([self.fireDate compare:afterDate] == NSOrderedDescending)
        return self.fireDate;

    // The notification can have its own calendar, but the default is the current calendar:
    NSCalendar *cal = self.repeatCalendar;
    if (cal == nil)
        cal = [NSCalendar currentCalendar];

    // Number of repeat intervals between fire date and the reference date:
    NSDateComponents *difference = [cal components:self.repeatInterval
                                               fromDate:self.fireDate
                                                 toDate:afterDate
                                                options:0];

    // Add this number of repeat intervals to the initial fire date:
    NSDate *nextFireDate = [cal dateByAddingComponents:difference
                                                     toDate:self.fireDate
                                                    options:0];

    // If necessary, add one more:
    if ([nextFireDate compare:afterDate] == NSOrderedAscending) {
        switch (self.repeatInterval) {
            case NSDayCalendarUnit:
                difference.day++;
                break;
            case NSHourCalendarUnit:
                difference.hour++;
                break;
            // ... add cases for other repeat intervals ...
            default:
                break;
        }
        nextFireDate = [cal dateByAddingComponents:difference
                                            toDate:self.fireDate
                                           options:0];
    }
    return nextFireDate;
}
@end

And this code works perfectly, But if I change the time zone then it starts to trouble, I don't know how to make it work when timezone changes.

I am setting defaultTimeZone in my UILocalNotification

here my UILocalNotification looks like

{fire date = Friday, July 8, 2016 at 6:30:00 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = NSCalendarUnitHour, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday, July 8, 2016 at 6:30:00 PM Central Daylight Time, user info = {}

1
Please be more specific what you mean about changing the time zone - Feldur

1 Answers

0
votes

When you create a new LocalNotification try to use this

localNotification.timeZone = [NSTimeZone systemTimeZone];