我想在這裏設置鬧鐘。我位於蒙特利爾,所以在EST時區。 在我使用的代碼中,我得到當前日期,並在幾分鐘後嘗試使其響起。代碼工作得很好,並且鬧鈴按預期振鈴。UILocalNotification&NSTimeZone,顯示錯誤的時區
這是問題:現在是12點41分。鬧鈴將在12.43點響起。 然而,在我的NSLog,時間印:fireDate:2012-02-16十七時43分00秒+0000
這不是一個大問題,因爲它的工作原理,但爲什麼它在顯示任何想法那個時間,仍然有效?任何想法如何解決這個問題?謝謝!
我基本上把時區隨處可見,這裏是我使用的代碼:
- (空)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];
}
謝謝!
哦,我不知道+0000,謝謝!但是,我添加了_dateComps.timeZone = [NSTimeZone timeZoneWithName:@「EST」]; _在dateComps init之後,我仍然獲得** fireDate:2012-02-16 18:07:00 + 0000 ** – 2012-02-16 18:06:45
對不起,EST不是有效的名稱。嘗試「美國/蒙特利爾」(編輯回答)。您可以使用[NSTimeZone knownTimeZoneNames]獲取有效名稱的數組。 – MGA 2012-02-16 18:14:45
有用!我複製/粘貼使用[NSTimeZone knownTimeZoneNames]找到的那個,到處都是......但我奇怪地仍然有+0000。我在設置每個變量之前移動了setTimeZones,並且仍然可以得到它! – 2012-02-16 18:24:40