Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect when the "Minute" changes on the status bar clock?

Tags:

ios

iphone

ios4

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!

like image 668
Louie Avatar asked Nov 12 '11 19:11

Louie


Video Answer


2 Answers

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.

like image 73
mja Avatar answered Nov 06 '22 13:11

mja


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.

like image 23
SG1 Avatar answered Nov 06 '22 13:11

SG1