Reputation: 2982
I have to set an UIAlertNotification at 6:00 pm local time. The time is set at 11:00am and will get to 18:00 pm because the local time zone I am in right now is GMT+6:00. However, it will be different in different time zones. I want it to be 6:00pm at any time zone. Can anyone kindly help me out? Thanks.
NSDateComponents *comps=[[[NSDateComponents alloc]init]autorelease];
[comps setYear:[year intValue]];
[comps setMonth:[month intValue]];
[comps setDay:[day intValue]];
[comps setHour:[@"11" intValue]];//6pm
[comps setMinute:0];
[comps setMinute:0];
[comps setTimeZone:[NSTimeZone localTimeZone]];
NSCalendar *cal=[[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date1=[[cal dateFromComponents:comps]retain];
//local alert code
NSInteger minutesAhead = 0;
NSTimeInterval seconds = (60 * minutesAhead) * -1;
NSDate *actualFireDate = [date1 dateByAddingTimeInterval:seconds];
NSMutableDictionary *dictC = [NSMutableDictionary dictionaryWithDictionary:userInfo];
[dictC setObject:date1 forKey:@"PCOriginalReminderDate"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = actualFireDate;
notification.alertBody = message;
notification.userInfo = dictC;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
Notes:
When I print the date, it says:
date = "2012-04-04 18:00:00 +0000";
I think it means that 11am was GMT and by adding 7 hours (since its GMT +7 - daylight) and makes it 18 pm.
Upvotes: 0
Views: 2038
Reputation: 16725
You want to set the timeZone property of your UILocalNotification
:
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.
So if you set your notification's fireDate
to the time you create in your snippet, and set its timeZone
to [NSTimeZone localTimeZone]
then you'll be good to go.
Upvotes: 2