I want to fire a UILocalNotification
in a specific Date. If I use this code:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:4];
[components setMinute:0];
NSDate *fireDate = [gregorian dateFromComponents:components];
If now it's 3 pm this works fine, but it doesn't when, for example, it's 5 pm.
How can I set the notification fire date to "the next 4 pm" ?
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:4];
[components setMinute:0];
NSDate *fireDate = [gregorian dateFromComponents:components];
NSLog(@"Fire date : %@",fireDate);// Today 4pm is already passed. So you wont get notification
// You need to check if the time is already passed
if ([fireDate compare:[NSDate date]] == NSOrderedAscending)
{
// Schedule it for next day 4pm
components.day = components.day+1;
fireDate = [gregorian dateFromComponents:components];
}
NSLog(@"Fire date : %@",fireDate);