Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone register for clock updates

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.

like image 211
MechEngineer Avatar asked Jul 09 '12 21:07

MechEngineer


People also ask

Why is my clock not updating on iPhone?

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.

Will my iPhone automatically change time for daylight Savings?

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!

Why can I not change my Date and time?

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.

Is clock an app on iPhone?

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.


1 Answers

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];
like image 159
beggs Avatar answered Oct 26 '22 08:10

beggs