UILocalNotification 总是将日期设置为本地时区 [英] UILocalNotification always setting the date to local timezone

查看:33
本文介绍了UILocalNotification 总是将日期设置为本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method that makes the store of a 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 总是增加一小时,为什么以及如何增加?感谢您的帮助.

Despite uncommenting the timezone setting, the UILocalNotification is always incrementing by one hour, why, and how ? Thank you for helping.

推荐答案

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

The date you pass in in GMT, which in you case is does not match your local time zone.

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

So when you set the date to 17:00 your time corrects it to you timezone (CET) which is GMT+1. Thus an hour gets added to you date.

一个解决方案是设置UILocalNotification 时区到 GMT:

A solution is to set the UILocalNotification timezone to GMT:

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

来自 Apple 文档:

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

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.

这篇关于UILocalNotification 总是将日期设置为本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆