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

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.

Related

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 continue monitoring iBeacon when screen is off in iOS?

I am developing an iOS app to monitor iBeacons. It works well in both foreground and background. I also need it to keep monitoring even when the screen is off. Now my problem is, when I turn off the screen with the shoulder button, NSLog shows that the iBeacon signal goes off (RSSI=0, beacon.accuracy=-1.0) accordingly, and 10 seconds later, there is no beacon found at all, while the delegate method locationManager:didRangeBeacons:inRegion: is called continuously. It seems that the app is running when the screen is off but iOS turns off the location service for iBeacon ranging. I tried to set "locationManager.pausesLocationUpdatesAutomatically = NO;" but still doesn't work. Is there any advice on this issue? Thanks in advance.
It seems that you're doing ranging instead of monitoring. Ranging only works when the app is active—either in the foreground or in the background. The catch is, iOS will automatically put an app to sleep a few seconds after you navigate away from it, or lock the screen.
What you have to keep in mind is, there's really no such thing as "permanent background state" on iOS. Apps run in the background for some limited time while transitioning to the suspended state (to let them, e.g., finish uploads/downloads), and there are also Background Modes that sometimes enable an app to run in the background, but these are usually reserved for very specific apps—e.g., the "audio" Background Mode means an app can be running in the background as long as it plays music, etc. There is a Background Mode for "location" services, but it's reserved for turn-by-turn navigation apps.
Beacon monitoring however continues running even if your app goes to sleep, or gets terminated by the iOS due to memory pressure. When you go in or out of range of a beacon, iOS will launch your app into the background to handle the event, giving it a few seconds (which you can extend up to a few minutes with a Background Execution Task) to do so (you can range during that time), but then it's back to sleep.
You might also want to take a look at this question:
Receive signal from beacon while app is in the background

How to set a timer to run when an app is both in foreground and in background?

I've my app set to keep receiving location updates while in background ("Background modes > Location updates"). My app listens for location updates while in foreground as well. But I need my app to keep listening for locations during a certain number of seconds I'm given, and to show a countdown if the app is in foreground, and to keep the time count if the app goes to background because I need to stop the location manager when the time ends.
I found no problems in setting an NSTimer when the app is in foreground, and showing a countdown, but I don't know how to handle it when app goes to background... in comments in AppDelegate's delegate applicationWillResignActive: and applicationDidEnterBackground: methods it is said that timers should be disabled and invalidated there... in addition, the timer I'm using is created in main thread:
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(updateCountdown:)
userInfo:nil
repeats:YES];
How could/should I manage this scenario?
Thanks in advance
EDIT: I'm going to try to explain an scenario I could have: my app listens for locations during a given time interval, and then it should stop listening. Let's say I'm going to give the user 5 min to reach certain location. User taps a kind of "start" button, and then I start the location manager to keep track of the user's positions, and I also start a countdown. When the countdown reaches 0, I stop the location manager and check the path the user has followed and the location he has reached. Since the app could go to background after the user has started the countdown, I have enabled Background modes > Location updates. Listening for locations while the app is in background is ok but, what if the countdown finishes while the app is still in background? I need to stop tracking user's positions even if the app is in background. I need a timer to keep running in background whatever countdown I need to set for the user, it could be 5 min, it could be 1 hour...
Thanks again
You can't run a timer in the background forever, it's not one of the background modes allowed by the OS so it will get terminated at some point. Wrapping it in a background task will only buy you a few extra minutes (about 10) at the most.
To properly track time when your app enters the background you need to:
From applicationDidEnterBackground
Save off a time stamp as soon as your app transitions to the background.
Terminate your timers
When your app re-enters the foreground:
From applicationWillEnterForeground edit or 'didUdateLocations'
Read the save time stamp
Use timeIntervalSinceDate to get the lapsed time in seconds
Restart your timer using the time span to set the remaining time
I think this kind of control needs a middleware to operate perfectly. I don't know if your app has backend or not but if it has, you may send a request when user tries to start timer in your app and you do your updates background of your app. After timer finishes backed can send a notification to user about this and you handle it if user comes back to your app or you leave it that way.

NSTimer when screen is locked

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".

How to know the reason my app is killed

I'm making a background tracking GPS app for iOS 7.0+.
I'm having issues when tracking position while in background, it's killed after exactly 5 minutes, even if we are in the middle of a highway, with 1 location per second. It's not killed when constantly in active state.
This seems to occur only on iOS 7.0 version, not on later versions.
I already registered the plist location background activity.
I need maximum accuracy and shortest update time, so I'm not using the significant change method but the basic startUpdatingLocation. This application is used mainly plugged inside a car (no mercy for battery).
I set the location delegate to the app delegate so it's less subject to be deallocated.
Even with all of this done, it's still impossible to take this app alive in background more than 5 minutes.
I'm fighting on iOS which always find a way to terminate my app, even with app state restoration.
So the question is, is there a way to know why my application is killed ?
List of threads that didn't help me or does not respond to my needs, and that I already visited :
How to keep GPS app running in background without pauses
iOS Multi-Tasking Track GPS Location
Periodic iOS background location updates
The system will not tell you why the app was terminated.
I set the location delegate to the app delegate so it's less subject to be deallocated.
The delegate has no effect on whether the location manager is deallocated or not. You must keep a strong reference to the location manager itself.

Resources