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

时间:2013-08-29 12:51:19

标签: 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总是递增一小时,为什么以及如何? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:7)

您在GMT中传递的日期,在您的情况下与您当地的时区不符。

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

解决方案是将UILocalNotification时区设置为GMT:

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

From the Apple documentation

  

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