ios - UILocalNotification 始终将日期设置为本地时区

标签 ios uilocalnotification

我有一个方法可以存储 UILocalNotification:

- (void)save:(NSString *)program at:(NSDate*)date  {

    NSLog(@"date to save: %@", date);
   // NSLog(@"timezone: %@",[date descriptionWithLocale:[NSLocale systemLocale]]);


    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    NSLog(@"date to fire: %@", date);
    localNotification.fireDate = date;

    NSString *s=[program stringByAppendingString:@" is now on "];
    NSString *title=[s stringByAppendingString:channel];
    localNotification.alertBody = title;

    localNotification.alertAction = @"Show...";
    //localNotification.timeZone = [NSTimeZone localTimeZone];


    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    NSLog(@"notification to save %@",localNotification);

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

    // Dismiss the view controller
    [self dismissViewControllerAnimated:YES completion:nil];
}

我的输出是:

date to save: 2013-08-29 17:00:00 +0000
date to fire: 2013-08-29 17:00:00 +0000
notification to save <UIConcreteLocalNotification: 0xc0c4240>{fire date = Thursday, August 29, 2013, 6:00:00 PM Central European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, August 29, 2013, 6:00:00 PM Central European Standard Time, user info = (null)}

尽管取消注释时区设置,但 UILocalNotification 始终递增一小时,为什么以及如何? 感谢您的帮助。

最佳答案

您在 GMT 中传递的日期与您本地的时区不匹配。

因此,当您将日期设置为 17:00 时,您的时间会将其更正为您所在的时区 (CET),即 GMT+1。 因此,您的日期增加了一个小时。

一个解决方案是设置 UILocalNotification格林威治标准时间的时区:

localNotification.timeZone = [NSTimeZone timeZoneWithName@"GMT"];

From the Apple documentation :

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

关于ios - UILocalNotification 始终将日期设置为本地时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18511533/

相关文章:

ios - 如何在 SKAction 中途反转 Sprite 所遵循的路径方向?

ios - 如何知道哪个对象发送了通知

ios - UILocalNotification 2 分钟后重复提醒

ios - 无法在 XCode 6.1.1 中更改字体

c++ - 如何在 XCode 4 中将旧式构建日志设置为默认构建错误 View

ios - Obj-c中iVar和self.iVar的保留计数是多少?

ios - 如何在 objc 中以重复间隔每 15 天触发一次 UILocalNotification

ios - Objective-C:UILocalNotification

ios - 应用程序在后台或关闭时的自定义通知

ios - 应用未运行时 UILocalNotification 会触发吗?