UILocalNotification始终将date设置为本地时区

我有一个方法,使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)} 

尽pipe没有注明时区设置,UILocalNotification总是增加一个小时,为什么以及如何? 感谢您的帮助。

您以GMT格式传入的date,与您当地的时区不符。

所以当你把date设定为17:00的时候,你的时间会更正你的时区(CET),这是GMT + 1。 因此,一个小时将被添加到你的date。

一个解决scheme是将UILocalNotification时区设置为GMT:

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

从Apple文档 :

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