CLLocationManager while app is in background state - iphone

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.

Related

When Standard Location Service and Significant Location Changes are used together, What`s happen?

Simultaneous use of Standard Location Service and Significant Location Change.
I’m trying to develop App using Location Information.
Can Standard Location Service and Significant Location Changes be used together?
I currently use Standard Location Service in the main.
However, Even when the app is in not running state, I want to use a location service in my app.
So I am trying to use not only Significant Location Changes but also Standard Location Service.
Then some questions came up,
When Standard Location Service and Significant Location Changes are used simultaneously, what`s happening?
Do the two functions work simultaneously?
Does only one function work? Assumption
If only one function works, which one will work?
If the two functions work simultaneously, Is there a problem with battery consumption?
The significant-change location service offers a more power-friendly alternative for apps that need location data but do not need frequent updates or the precision of GPS. This can run when the application is in foreground, background or not running state. So if your application does not need very frequent locations updates then you can use this. FYI it delivers location updates to your app only when the user’s position changes by a significant amount, such as 500 meters or more.
Standard location service will work when the application is running in foreground state and background state(when the back ground mode for location is enabled) only. As soon as the application will get killed(by user or iOS) the standard location service will stop working.
For this you have to set the significant location change service which will wake your application in the background and provide the location updates to your application.
So you should analyse the functionality in your application and check the required frequency of the location and decide your implementation.
Both do not work simultaneously. Only one will work according to the application's requirement and application state.
Both services will work according to the previously mentioned.
They do not work simultaneously. But if you choose significant location changes to implement then it will be far more battery efficient but the location fetching accuracy will be very low.

UIBackgroundModes location and significant location changes with region monitoring

I have an app that uses a combination of startMonitoringForRegion: and startMonitoringSignificantLocationChanges to keep aware of where the user is when the app is in the background. Does this mean that I need to include the location value for the UIBackgroundModes key in the Info.plist?
This is a quote from the docs:
The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.
...
An app that provides continuous location updates to the user (even when in the background) can enable background location services by including the UIBackgroundModes key (with the location value) in its Info.plist file. The inclusion of this value in the UIBackgroundModes key does not preclude the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur.
My interpretation of this is that the location value for the UIBackgroundModes key is only required if the app needs continuous location updates, like a sat nav app.
I have also tried running the app on a device without the location value for the UIBackgroundModes key and it continues to report significant location changes and when the a region is entered of exited.
Also, the only place that UIBackgroundModes is mentioned in the CLLocationManager Class Reference is in the startUpdatingLocation discussion, which I am not using.
You're right about the location key, it's only required when your app needs high-precision location updates even when in the background. Something like Runkeeper uses this to allow it to keep tracking your location, even when you're using another app with multitasking. From the docs for iOS Keys: UIBackgroundModes
"location": The app provides location-based information to the user and requires
the use of the standard location services (as opposed to the
significant change location service) to implement this feature.
And
Where alternatives for running in the background exist, those alternatives should be used instead. For example, apps can use the signifiant location change interface to receive location events instead of registering as a background location app.
Region monitoring will work without the location key. In fact, region monitoring will work without any special iOS keys being enabled.
You say that you're not using CLLocationManager, but if you're using region monitoring, you'll have to use that class. You need to set up a location manager delegate for your app to actually get the region notifications.
Yes, you must need to add "location" key under Backround modes in Info.plist if you are using significant change location service (startMonitoringSignificantLocationChanges) to monitor user location in the background and in the app kill state.

Use of Standard location service for tracking traveled distance in a foreground/background app

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.

iPhone: stopMonitoringSignificantLocationChanges when app quits

I want to stopMonitoringSignificantLocationChanges when the user is killing the app from the multitask bar (switch bar) like the Waze GPS App does.
(i also want to cancel all LocalNotifications)
the problem is, that after an application get's suspended, applicationWillTerminate doesn't get called.
The way Waze does it is by running the NORMAL LOCATION SERVICE in background, not the "significant location service".
a special option that can be set in the plist file.
there is no way to close the Low Power mode of location services - "Significant location service" when the user kills the app, because it was not intended to be used this way.
I guess I will simply have to follow apple's standards using significant location service.
did you have a look at this post: Behaviour for significant change location API when terminated/suspended?
Lots of interesting things in it.
In particular, you should stopMonitoringSignificantLocationChanges on entering background and register for significant location changes with a Service

What can I do inside "location" background thread?

For the sake of this question, assume I plan to build a Google Latitude client app for iOS 4.
my app needs to upload the user gps location every two minutes, and also download the user friends locations. - in the background!
my app can't wait to be woken up by the OS on cellular tower switches (because they may only happen after 2 KM, while my app needs constant gps updating), so I understand I can create a thread, similar to the ones GPS navigators use, to run in the background.
I have no knowledge in iPhone programming, I just need to know if my app is feasible.
Will I be able to access the internet from within the background thread (and upload the user gps location), or is it restricted to only sampling the GPS location.
1.
Will Apple approve such an application, or is this type of use forbidden? because my app isn't realy critical such as a gps navigator
Thanks!
As Don said, iOS will send location events to apps that support it. From there, the app can do what it needs to do with the location event.
Background apps can register for one of three options:
Significant changes only: a low-power way to track location that only wakes the app up when there's a "significant" change in location
Standard location services: compatible with all iOS devices that can use location services, allows you specify change in distance, and works in background. However, if the app has been terminated or suspended, iOS won't attempt to relaunch the app.
Continuous location services: allows the program to receive continuous location updates and iOS will not suspend the application when sent to the background.
More info: Executing Code in the Background and Location Awareness Programming Guide
If you take a look at Executing Code in the Background, Apple provides guidelines on what multitasking-aware apps should and shouldn't do.
You will receive the locationManager:didUpdateToLocation:fromLocation: message in your app. In there, you can do whatever you need to, although you should not update the UI or use Open GL if your are in the background.