Does NSTimer works in conjunction with device's clock? - ios

If we manually change the clock time of device, will the NSTimer output change? I want to show a count down, but I am not sure if user will change the device in-between then will it change the NSTimer outcome too ?

Related

How to Change AccessibilityLabel on runtime in iOS Swift?

I am showing a 30 min timer in my iOS application, I want to change the accessibilityLabel of the timer on runtime so that whenever the user taps it, the talkback will read the latest time. Right now the Label gets set to 30 min and whenever the user taps on the timer the talkback reads 30 min.
Try :
UIAccessibility.post(.notification: .screenChanged, argument: timer.text)
You want to use a .screenChanged notification because whenever the timer changes, the screen is changing as well. Your argument is the text that is changing.

How to keep a timer counting when app reach background

I prepared a CountDown timer for Pomodoro technique. I would like to know how don't pause the app when it reach a background. I have a method which update UILabel from 20min to 0 by 1sec. When Timer reach 0 it should play the sound and vibrate device. All works fine when app is launched in foreground, but how to do it at background? Is it possible to track timer change when app is in background mode?
BR
iMat
The short answer is no. A timer on a VC will not continue to run when the app is in the background because it goes into suspended mode.
You could schedule a local notification to fire when the app is in the background, but as far as updating the UI label, you'll have to update that when the user comes back into the app.
Invalidate the timer when the app goes to background. Store the remaining time remainingTime and current time backgroundTime. (You can get the current time using Date())
Compare the current time backToForegroundTime when the app comes back with backgroundTime. Subtract them to get the time elapsed timeElapsed.
If timeElapsed is less than the remainingTime, subtract that amount from remainingTime and create the timer again with the new duration.
You can use my approach from this gist. Just create repeating timer and update what ever you want in repeating block, and handle timer finishing in other block on main queue or background queue!
Glad to help with questions!
Apple has defined a specific set of tasks, an app can perform when in background.
Running a timer, unfortunately, is not one of them.
Read Background Execution section of app programming guide for more details.
Most apps, intending to continue to execute code in background, implement one of the allowed long running background modes, even if it is not required for your apps actual functionality, and use them to execute their code.
But be ware, you will be doing something apple specifically asks you not to do. This could result in app store rejection if found.

How to reload a view in the apple watch?

Hi every one i have to update instantly from my parent app (iPhone App)
and Show the value in the watch App. I have to reload the value in the
watch screen when ever the apps values get updated. I have to update from the app and show it in its extension app.
if change the location in the iPhone app. Automatically the values should be changed in watch App. I'm using App Group and NSUserDefault to share values between these apps.Now i can access values from the watch App but its not get updated automatically. Thanks in advance
You can set a timer and change the contents of labels.
In WatchKit (WKInterfaceLabel), setting label's contents is very easy. Just use the following code in order to set a new text for labels:
Swift:
label1.setText(temperature)
Objective-C:
[label1 setText:temperature];
Also you can do the same with pictures, charts etc. Just insert the code said before in a function, and call that function each time the timer finishes its interval time.
It's better to set short intervals for timer. Less interval means refreshing faster but more internet usage and battery drain, and longer intervals means refreshing not fast as previous way, but less internet usage and more battery saving.
You can change the interval in run-time by battery percentage. If it is - for example, less than 20% - you can set longer intervals, and when it is more than 20%, set shorter ones. You can only find Apple Watch battery percentage in watchOS 2, but in both versions you can get iPhone battery percentage by the following code:
Swift:
var x = UIDevice.currentDevice().batteryLevel
Objective-C:
int x = [UIDevice currentDevice].batteryLevel;
NOTE: Short intervals are from 10ms to 200ms, and long ones are from 200ms to 1s. (ms = Milliseconds = 0.001 seconds)
NOTE: You can call the initialization method of the view in timer intervals.
If you want to update view manually, just make a button, and set labels and charts' data (by the code given before) or call the view init method.

How to keep the NSTimer always work evenly

Now I am creating a metronome program. I use a NSTimer to bring the metronome into play. Of course The Timer works repeatedly. But I find out in two situation that the timer works not accruately.
When just start the NSTimer, the first two beat sometimes goes too closely. After then, the beat goes evenly.
When the app goes backgound I make the Timer work continuely by:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
but sometimes the unevenly beat also happens at the time when entering or coming back from the background status.
So I want to know how to keep the Timer always work evenly, no matter which situation it is in. Thks!
When setting up a timer firing at a certain time, the system implicitly sets an allowed "leeway" which is a short delay within the timer may actually fire.
This is due to "Timer Coalescing" - a system feature - which groups events happening roughly at the same time together and fire them at the exact same time. The reason is to reduce CPU cycles and extend the idle time of the CPU to save power.
If the timer interval is small (milli seconds to seconds) this "leeway" is in the range of 10% of the timer interval.
You can explicitly retrieve and set the current maximum allowed leeway value with the methods
-tolerance
-setTolerance:
See also:
NSTimer
Timing Accuracy

How to Make and automatic timer reset in Delphi 7?

Could someone tell me how to make a timer Reset after a given time ? Like i'm trying to make a clock , and i would like it to reset , and start over (like after 60 min , and start over from 0).
Have your OnTimer event update the timer's Tag property, or a separate variable, with how much time has elapsed, and then reset the timer when that value reaches your threshold.
Or you could simply run a separate timer that triggers one time after your desired interval elapses, and then reset both timers together.

Resources