我想在这里设置警报。我位于蒙特利尔,所以在 EST 时区。在我使用的代码中,我得到了当前日期,并尝试让它在几分钟后响起。该代码运行良好,警报按预期响起。
问题是:现在是上午 12.41。闹钟将在 12.43 响起。但是,在我的 NSLog 中,打印了时间:fireDate:2012-02-16 17:43:00 +0000
这不是一个主要问题,因为它可以工作,但你知道为什么它当时显示并且仍然有效吗?关于如何解决这个问题的任何想法?谢谢!
我基本上把时区放在任何地方,这是我正在使用的代码:
-(void)scheduleNotificationWithInterval:(int)minutesBefore {
// Current date NSDate *now = [NSDate date]; // Specify which units we would like to use unsigned units = NSTimeZoneCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSTimeZone* zone = [NSTimeZone timeZoneWithName:@"EST"]; [calendar setTimeZone:zone]; NSDateComponents *components = [calendar components:units fromDate:now]; [components setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]]; NSInteger year = [components year]; NSInteger month = [components month]; NSInteger day = [components day]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setYear:year]; [dateComps setMonth:month]; [dateComps setDay:day]; [dateComps setHour:hour]; [dateComps setMinute:minute+2]; // Temporary NSDate *itemDate = [calendar dateFromComponents:dateComps]; NSLog(@"fireDate : %@", itemDate); UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; //localNotif.timeZone = zone; localNotif.timeZone = [NSTimeZone timeZoneWithName:@"EST"]; minutesBefore = 15; // Temporary localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil), @"Blabla", minutesBefore]; localNotif.alertAction = NSLocalizedString(@"See Foo", nil); localNotif.soundName = UILocalNotificationDefaultSoundName; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"LastCall" forKey:@"lastcall"]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
谢谢!