I have a complex problem to describe so Ill save it and just sum up what im trying to do. I am looking to "get notified" when the time on the status bar changes so I can recalculate time till. I am currently calculating time till just fine, but there is that 1 minute window where my calculations and where the time stamp wont match... it all depends on when they opened the app, compared to where the iPhone "seconds" clock was when they opened it.
So in short, can we detect when the minute changes on the status bar? If so, how?
Thanks!
Even simpler than Emilio's answer - when your view loads (or when you want to trigger the event) just check the seconds portion of current date (eg. with NSCalendar), schedule a timer that will fire in 60-currentSeconds (that will be the next minute, zero seconds) and finally, schedule new timer that fires each 60 seconds.
Based on @mja's suggestion:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger currentSecond = [components second];
//+1 to ensure we fire right after the minute change
NSDate *fireDate = [[NSDate date] dateByAddingTimeInterval:60 - currentSecond + 1];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
interval:60
target:self
selector:@selector(YOURSELECTORHERE)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
I'm using it and it works great. You'll have to be more careful if you need to ever remove the timer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With