This question already has answers here:
Periodic iOS background location updates
(9 answers)
Closed 5 years ago.
I have an app that is used for tracking van drivers. I need to be able to get the location (x&y) every 30 seconds or so. I have a method and a timer that allows me to do this.
I was wondering how (if at all possible) I can have this working regardless of whether the app is active or not?
I've recently started messing around with core-location so my knowledge my not be bullet-proof.
If the app is is still active but only in the background then as long as you set the allowsBackgroundLocationUpdates to true then you will get updates. But this could become battery consuming, it could go on until your battery dies.
You may want to avoid constant location checking by setting:
pausesLocationUpdatesAutomatically to true.
However if you do set it to true then if your driver stops at a location for too long(I don't know what means too long) then the app would stop completely from tracking location until you bring the app to foreground again. I'm saying that based on:
Important For apps that have in-use authorization, a pause to location
updates while in the background ends access to location updates until
the app returns to the foreground and is able to restart location
services. If you do not wish location updates to stop entirely,
consider disabling this property and changing location accuracy to
kCLLocationAccuracyThreeKilometers when your app moves to the
background. Doing so allows you to continue receiving location updates
in a power-friendly manner.
So it's a little tricky. You may want to set pausesLocationUpdatesAutomatically to true but then if your business requirements allow...do something like stopUpdatingLocation() after 1hr of app being in background to stop location updates completely.
Yes, this is possible.
If your iOS app must keep monitoring location even while it’s in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. (In this situation, you should also make sure the location manager’s pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps.
For more information, check out the Location and Maps Programming Guide.
Related
I have to develop a kind of GPS navigation application that needs to constantly keep track of the position of the user, which is moving by car.
In the specific, I don't have to display the current location on a map but just to record its position with the best possible precision in order to calculate the total distance traveled.
Of course the application needs to continue its work in background if the user switch to another app, a phone call come in or something like that…
From the tests I have made and from what I have learned from this useful post (and from the documentation), it seems to me that the best choice in this case is the Standard location service, with the application configured with the UIBackgroundModes = location in the .plist file.
In this way it will continue to receive the location updates even if in background and it will never be suspended by the OS (this is actually true only if [locationManager startUpdatingLocation] has been called). This is also confirmed by this guys.
I have personally verified that is true simply by running the app with Instruments and the Memory monitor module where you can see the various flags about the app states, putting the app in background first with UIBackgroundModes set to location and startUpdatingLocation active, and after without it.
1) I'm now wondering what to do when the app is terminated when it is tracking the position. I don't want to loose any location updates so I need to wake up it again whenever a new update is available.
The documentation say:
Important: The applicationWillTerminate: method is not called if your
app is currently suspended.
But because in my case it will be never suspended (it will stay in background but not suspended), my logical conclusion is that applicationWillTerminate will be always called and so I could register for a Significant location update or Region monitoring inside of this method in order to be waked up and then restart the Standard location service.
Is applicationWillTerminate the right place to put this code?
2) An application working in background but not suspended could be terminated by the OS for no other reasons than a very low memory condition or for my app don't properly respond to a memory warning? (the user could also manually close it). I was concerned if applicationWillTerminate wouldn't be called in some way.
3) Could Apple not approve an app which constantly use the standard location services in background because of its quick battery drain?
Have you ever had approve problems for similar apps?
since you are asking many questions which you shoukd not do here, i cam give you only aswrs to a part of it.
if the app is terminated, then you cannot restart it anymore.
This is usually the case when the user terminates the app.
Dont worry Apple ( ios) will not terminate your app. your app will not use much memory, games with huge bitmap graphics are more likely to be termin.
evry gps app will drain the bat. that is not a reason for not aproving.
suspended means that app is not in background mode, it is sleeping. you will not receive Gps, so there is no need to call you on terminate. you have to save data before, see the apple docu on background modes and app life cycle.
My question is: Is CLLocationManager continue running, while my app is inactive?
Yes, if CLLocationManager is first called startUpdatingLocation method, and in the AppName-Info.plist file is added Required Background Modes -> App registers for location updates
Yes, it could. You have two options for handling location service events when your app is suspended, which can be read at the article: Getting the User’s Current Location.
As noted:
There are two different services you can use to get the user’s current location:
The standard location service is a configurable, general-purpose solution and is supported in all versions of iOS.
The significant-change location service offers a low-power location service for devices with cellular radios. This service is available only in iOS 4.0 and later and can also wake up an application that is suspended or not running.
Also, as noted at the bottom of this article in the section "Getting Location Events in the Background":
If your application needs location updates delivered whether the application is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your application at appropriate times to handle new events. However, if your application needs to use the standard location service, you can declare your application as needing background location services.
An application should request background location services only if the absence of those services would impair its ability to operate. In addition, any application that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation application would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.
There are some important subtleties with this (as of iOS 7.1):
The Location Update background mode should NOT be used if you are just looking for significant change and region enter/exit events. You will still receive these events even if the background flag is NOT set, and you will save a lot of battery at the same time.
If you do the above, you need to be mindful of limited permitted background time. If you don't take care to wrap up network requests etc. in the permitted time, you will network transaction failures.
You should ONLY use the location background mode if you need to use detailed location tracking (e.g. -startUpdatingLocation), in which case this background mode will keep your app awake.
Using the location background mode when not getting detailed location will piss off your users and may get your app rejected during the review process (depending on how you use location throughout your app).
Your app may be killed at anytime by the OS if you do not have the background location mode set, so you will need to be sure to properly re-initialize your CLLocationManager instance in applicationDidFinishLaunching or applicationWillFinishLaunching in order to get the subsequent updateLocation or didEnter/ExitRegion delegate call. Just because location wakes up your app with a location update, it does NOT magically re-create your CLLocationManager without you programming it!
Hope that helps!
To disable CLLocationManager while the app is in backround mode you simply must not add the
"App registers for location updates"
in the
"Required background modes"
key of the info.plist file.
I suggest to use the significant-change location service instead of the standard location service whenever possible, to preserve device battery.
I have an app that need Location Based Services in the background.
So in info.plist I setrRequired background modes to "App registers for location updates".
and everything works fine.
My question is, how can I disable it running in background?
Is it possible to add an option and let users set it on/off?
As I know info.plist can not be change programmatically
so how can I do this?
The key is the description - it supports registering for location updates. So all you should have to do is tell your location manager to stop updating when the app goes into the background (or before) and it should then not be using location updates in the background.
There is no way to re-define the info.plist abilities of the app at runtime.
Traditionally (and pragmatically) you would set up your location manager to -startUpdatingLocation while your app is in use. This will drain your battery pretty quick if you leave it running full time. Typically you would call this, get the information you need, or update information, the call -stopUpdatingLocation. This puts everything to rest, no more battery drain.
When rolling over to the background, you have very limited options for accuracy if you still want to preserve battery life. The main one is -startMonitoringForSignificantChanges. This location update relies primarily on cell tower hand offs and triangulation. So if your user isn't moving large distances or is in an area with limited cell reception, don't expect this background mode to work very good.
You other background option is -startMonitoringRegion. This allows you to create a circle based region around places and get notifications when you -enterRegion or -exitRegion. These all have to be set up before entering the background. They do have the added benefit of better location updates. WiFi changing, cell tower handoffs, and even other apps using location updates. The OS grabs all locations updates and funnels them down and makes them available to any regions registered with the OS.
You still have the option to run location updates live in the background, but your users will not be thankful that they have no battery after 30 minutes of use.
Good luck. There are plenty of examples how to achieve all of these on SO and the web. The trick is finding the right combination that will work for you. Good Luck.
I am writing an application that records a users position at regular intervals whilst walking. I am using an NSTimers to schedule "startUpdatingLocation" followed by calling "stopUpdatingLocation" shortly afterwards to save as much battery as possible.
I want the user to be able to start the application and lock the phone thereby putting the application in an inactive state. My question is when this happens my application (when running via Xcode) seems to continue as normal, but I am curious if there are any differences in the way the application runs in this state as apposed to when the application is running as active?
From the docs it only mentions "applicationWillResignActive" with regards to the application passing through that state on its way to the background. I am more interested in how an application behaves when a used locks the UI and puts the phone away, I just want to make sure its going to keep doing what it should be doing or do I need to take extra measures?
When you app hits applicationWillResignActive it can continue to receive location updates if you use the UIBackgroundModes option in your apps info.plist.
Add the location key.
It is important that you do not use a timer in the background as timers will run but hold their "fire" until the app becomes active again. Thus you will only get one read when the user comes back to the app. The GPS location accuracy level is what will drive how much the battery is affected.
In your app description in the App Store, Apples requires:
Continued use of GPS running in the
background can dramatically decrease
battery life.
You app will not be allowed to go live until the above text is in the description for the user to read.
You should definitely be testing this on a device. The simulator doesn't auto-lock, for instance.
applicationWillResignActive is called when the user presses the home button, when another app fires a notification the user accepts (including when a call comes in), and when the device locks.
Look into the multitasking documentation, and what it has to say about background location updates.
My suggestion is that you conserve the user's battery by:
- starting a timer, which when fired, starts location updates (and stops the timer)
- when you get an adequately-accurate position record, stop location updates and restart the timer
- if location updates fail, restart the timer for a longer period. Maybe they're underground.
The most efficient approach is using significant location update service while your application is in the background or the screen is locked on your app. You might get one update every ten minutes or so.
Remember also, the user can disable your app's location services permissions. Especially these days...
Basically, I've been learning Objective-C and how to develop for the iPhone over the past few months and have created a few basic applications. Now I've got an idea for an iPhone app that I'm interested in developing and I'm just looking for a bit of advice on wether it is actually possible. I'm looking to create an app that sends the iPhones location to my server every 15 minutes - even when the app hasn't been opened/isn't opened.
Is this possible? If it is, could you point me in the direction of some more info on this subject.
Thanks.
You can declare your application as needing background location services. But this is available only in iOS 4, and user needs to allow location services for your app.
More info about multitasking can be found here
More info about getting users's location can find here
The answer to your question could be a partial YES or a partial NO. Basically ur app can give location updates even when it is not opened ... and u can handle it to be sent to ur server whenever user location updates .. but it is not possible to send the location update on a time basis like every 15 minutes ..
If you want ur app to register for location updates in background mode, u have two options:-
send location update on significant location change (i.e. a location update is sent when user moves significantly such that the cell tower changes for the user ) in this case u have to use startMonitoringSignificantLocationChanges OR
send continous location updates by using startupdatingLocation and in info.plist file declare background mode key for location updates (uses GPS)
and u have to handle didUpdatetoLocation for location updates in both cases and Ur app will be woken up in background as soon as a location update is recieved (beware of battery usage in 2nd case)