我想为特定的EST时间设置本地通知,用户应该收到通知而不管用户的默认时区。比方说,如果我希望本地通知在美国东部时间中午12点到达,那么太平洋时区的某个人也应该在中午12点00:00(太平洋标准时间09:00)收到通知
这是我试图做的,但是当我将默认时区设置为EST时,我才收到通知
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.repeatInterval = kCFCalendarUnitDay;
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:today];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:dateComponents.day];
[dateComps setMonth:dateComponents.month];
[dateComps setYear:dateComponents.year];
[dateComps setHour:12];
[dateComps setMinute:00];
[dateComps setSecond:00];
NSDate *fireDate = [calendar dateFromComponents:dateComps];
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
localNotif.alertBody = @"Daily Notification";
localNotif.alertAction = NSLocalizedString(@"View", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Daily Notification" forKey:@"acme"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
有人能告诉我哪里出错了。
答案 0 :(得分:1)
我在这里发现了问题。时区应该设置日期而不是通知,所以我必须添加这行代码:
[dateComps setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];
并且应该注释代码行:
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];