Currently, I have a countdown timer running. The code I use is below.
What I need help on, is finding out when the countdown is over (zero time left).
I can't quite figure it out. I need to get it so I can send a push notification when the countdown is over.
Thanks for the help!!
- (IBAction)startCountdown:(id)sender {
//Remove the time component from the datePicker. We care only about the date
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];
//Set up a timer that calls the updateTime method every second to update the label
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
}
-(void)updateTime
{
//Get the time left until the specified date
NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600) % 24;
NSInteger days = (ti / 86400);
//Update the lable with the remaining time
self.countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}
UPDATE This is what I've tried so far:
- (void) bothDatesEqual {
if ([_countdownLabel.text isEqualToString:@"00 days 00 hrs 00 min 00 sec"]) {
NSLog(@"No time left");
}
else {
NSLog(@"Some time left");
}
}
UPDATE 2 This is what I changed per DanH
- (void)startCountdownDate {
//Remove the time component from the datePicker. We care only about the date
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];
[self updateTime];
NSTimer *timerDate;
timerDate = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeDate:)
userInfo:nil
repeats:YES];
}
-(void)updateTime
{
//Get the time left until the specified date
NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600) % 24;
NSInteger days = (ti / 86400);
//Update the label with the remaining time
_countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}
- (void)updateTimeDate:(NSTimer *)timerDate {
NSDate *now = [NSDate date];
NSDate *targetDate = self.datePicker.date;
if (now == [now laterDate:targetDate]) {
// the current time is >= targetDate
[timerDate invalidate];
}
}