gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:11:14 25 4
gpt4 key购买 nike

我有一个方法可以存储 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com