A view in my application has a clock that I'd like to keep synced with the system. A number of Stack questions have centred around NSTimer, but before doing that, I want to check if there is a system notification I can register that is fired every minute.
Does such a thing exist? I'm looking through NSNotificationCenter but so far have nothing.
Make sure that you have the latest version of iOS or iPadOS. Turn on Set Automatically1 in Settings > General > Date & Time. This automatically sets your date and time based on your time zone. If a message appears saying that updated time zone information is available, restart your device and any paired Apple Watch.
If you have an iPhone, like the iPhone 14 Pro, go to the Settings app, select General, then Date & Time, and toggle on Set Automatically. Once that's done, your iPhone time will automatically update in accordance with any daylight saving changes — no extra work from you required!
Update Date & Time on Your Android Device Tap Settings to open the Settings menu. Tap Date & Time. Tap Automatic. If this option is turned off, check that the correct Date, Time and Time Zone are selected.
Clock is a timekeeping mobile app included with iPhone since iPhone OS 1, with iPad since iOS 6, and Mac since macOS Ventura. The app includes world clock, alarm, stopwatch, and timer functions. A Bedtime feature was added in iOS 10.
You can use NSTimer
's initWithFireDate
to fire on the minute by calculating the next change of the minute:
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ss"];
int currentTimeSeconds = [dateFormatter stringFromDate:currentDate].intValue;
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:60 - currentTimeSeconds];
NSTimer *updateTimer = [[NSTimer alloc] initWithFireDate:fireDate
interval:60
target:self
selector:@selector(updateSelector)
userInfo:nil
repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:updateTimer forMode:NSDefaultRunLoopMode];
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