1

我正在实施UILocalNotification以在晚上 8:00 提醒首次使用该应用程序的用户再次使用该应用程序。用户可以将其完全关闭或设置不同的时间。

我选择晚上 8:00 作为下班后的时间,这样它就不会那么打扰或打扰了。

我不确定如何处理时区。

如果用户在纽约。[NSDate date]仅返回 GMT 日期时间(自参考日期以来的某个时刻)。那么我如何确定他的晚上 8:00 呢?

我可以这样做吗?

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *dateComps = [calendar components:(NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitTimeZone) fromDate:[NSDate date]];

[dateComps setHour:20];
[dateComps setTimeZone:[NSTimeZone defaultTimeZone]];

NSDate *result = [calendar dateFromComponents:dateComps];
4

1 回答 1

3

UILocalNotification 文档

fireDate 中指定的日期将根据此属性的值进行解释。如果您指定 nil(默认值),则触发日期被解释为绝对 GMT 时间,这适用于倒计时计时器等情况。如果您为该属性分配一个有效的 NSTimeZone 对象,则触发日期将被解释为挂钟时间,当时区发生变化时会自动调整该时间;适合这种情况的一个例子是闹钟。

因此,为了确保您的通知设置在相同的“挂钟”时间(例如晚上 8 点),无论时区如何,只需执行

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = theDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
于 2013-11-21T00:07:50.610 回答