使用不同的时区确定下次开火日期的UILocalNotification

时间:2016-07-08 13:12:16

标签: ios objective-c nsdate nsdateformatter uilocalnotification

我想找到UILocalNotification的下一个开火日期,这里是代码

@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

这段代码完美无缺,但是如果我改变时区就会开始出现问题,我不知道如何在时区发生变化时使其工作。

我在defaultTimeZone

中设置了UILocalNotification

这里我的UILocalNotification看起来像

  

{开火日期=星期五,   2016年7月8日下午6:30:00印度标准时间,时区=   亚洲/加尔各答(格林威治标准时间+5:30)偏移19800,重复间隔=   NSCalendarUnitHour,重复计数=   UILocalNotificationInfiniteRepeatCount,下一个开火日期=星期五,   2016年7月8日中部夏令时下午6:30:00 ,用户信息= {}

1 个答案:

答案 0 :(得分:0)

创建新的LocalNotification时尝试使用此

<div>
  <div>
    <input id="search_query" type="text" placeholder="Insert the text" /> 
  </div>
  <div><button id="search" type="submit">Go</button></div>
</div>

<div id="results"> </div>