【问题标题】:Determine UILocalNotification next fire date with different timezone确定具有不同时区的 UILocalNotification 下一个触发日期
【发布时间】:2016-11-11 01:54:15
【问题描述】:

我正在尝试查找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

而且这段代码运行得很好,但是如果我更改时区就会开始出现问题,我不知道如何在时区更改时让它工作。

我在我的UILocalNotification 中设置defaultTimeZone

这里我的UILocalNotification 看起来像

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

【问题讨论】:

  • 请更具体地说明您更改时区的意思

标签: ios objective-c nsdate nsdateformatter uilocalnotification


【解决方案1】:

当你创建一个新的 LocalNotification 时尝试使用它

localNotification.timeZone = [NSTimeZone systemTimeZone];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多