0

我有一种方法可以存储 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 总是增加一小时,为什么以及如何?感谢您的帮助。

4

1 回答 1

7

您在 GMT 中输入的日期,在您的情况下与您当地的时区不匹配。

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

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

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

从苹果文档

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

于 2013-08-29T13:29:32.827 回答