NSTimer when screen is locked - ios

How do I keep the NSTimer persisting after the screen is locked in iOS 8? Every time I lock the screen it stops. I have tried putting it in an NSRunLoop to no luck.

iOS always suspends NSTimers when the app is backgrounded. You can request some extra background time by using UIApplication beginBackgroundTaskWithExpirationHandler: but be warned that it won't keep your timer going forever. Your best bet is to remember what time you are backgrounded in applicationWillResignActive and then check the current time when you are re-activated in applicationDidBecomeActive or applicationWillEnterForeground. Then you can calculate how much time passed when your app was inactive and do whatever is appropriate.
See the UIApplication docs here https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html, particularly the section on "Managing Background Execution".

Related

In a iOS app, if a timer is scheduled to fire and the app is in background, am I certain that it will fire as soon as the app goes foreground again?

I a swift iOS app, I have a timer that fires every day (86400 seconds). I have tested the behaviour of the timer when the app is in background, and it seems to me that in that situation, the timer will fire only when the app goes into foreground again. Am I guaranteed of this behaviour ? I want to be certain the timer does fire. It is fine for me if it fires when the app goes in foreground again, as long as it fires then. Thanks
The short answer is that an application can continue "executing" in the background indefinitely for only a limited number of reasons. Aside from these specific reasons, the app can ask for short periods of time to continue executing in the background. Once these periods are over you tell iOS you are done, in which case the app is suspended, or iOS will eventually forcibly terminate your app (it depends on resources).
So, firing a timer is not one of those specific reasons. However, the processing you are doing with the timer could be!
Your app will briefly be in the background on its way to being suspended. When it is suspended, it is not executing anything -- including the timer.
If your app is terminated (swiped from memory or shutdown by iOS), it isn't coming to the foreground, it is being launched again. And your timer is firing because you are launching your app.
If your app stays suspended, it will come to the foreground. And your timer will fire because of the time interval involved.
Either way you can guarantee that your timer fires.

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.

Does code runs in background mode in iOS based on timers?

I started a timer when app goes into background when selector method called app got crashed in background. I am not sure does code works in defined conditions.
In background mode, when your app is not running, timers will just not fire. A timer will fire at the earliest time it can (for example just after your app is activated again). If a repeating timer should have fired more than once, only the last "fire" will happen.
This shouldn't make your app crash; whatever goes wrong is something else.
Timers only run in the background if you app supports running in the background, like when tracking location, playing music, etc.

iOS long-running background timer with "location" background mode

I want to implement long-running background timer which sends user's location to server periodically.
To do this, I set location mode for UIBackgroundModes, and call beginBackgroundTaskWithExpirationHandler when the application goes to background.
And for CLLocationManager, I use startUpdatingLocation method (don't want to use significant change, because I need high precision location).
But around 26minutes after the app goes to background, it stops to send location to the server. But the app isn't crashed, so after I bring the app to foreground, it can resume its timer.
26minutes is from experiments, with iOS 6.1, iPhone 5.
Here are some questions,
I can't understand why it suspends after 26mins not 10mins, which is known as time limit for background task.
Before 16mins, backgroundTimeRemaining methods returns double max value. But after 16mins, it decreases from 600, so it suspends the background task after 10mins.
I already tried to call beginBackgroundTaskWithExpirationHandler inside of expiration handler, but no use.
If there's anyone who has a clue for this problem, will be greatly appreciated.
https://github.com/voyage11/Location
Use this code.you can set time interval you want to call it. I have tried many codes, but i found this as most accurate and least battery issue.This is also awesome for background location service. :)
if you have set UIBackgroundModes successfully,the App can run a long time in the background while you are moving. But, if you stop moving for over 10 minutes(maybe more), the App will be suspended by the system. Your App will be awake if the location updates,but this awake time will be very short,so you must cal
beginBackgroundTaskWithExpirationHandler method to handle location update.

iOS Application idles and then locks

What delegate is called in this situation if I have an app running.
Over time, if iPad is set to lock after 2 mins, what is its state? Does the app still run? or does it suspend?
I'd like to know thanks
If the iOS device automatically locks after a period of inactivity, the same thing happens as when it's locked by pressing the lock button: the app briefly goes into the background before being suspended. (Unless of course it is an audio-playing app or other app that requests extra execution time, in which case it may stay in the background longer, or indefinitely, without being suspended).
In terms of UIApplicationDelegate methods, applicationWillResignActive: will be called first, followed by applicationDidEnterBackground:.

Resources