Applications can register for significant location changes.
(Recommended) The significant-change location service offers a
low-power way to receive location data and is highly recommended for
applications 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 applications or
applications that provide the user with noncritical, location-relevant
information. If the application is suspended when an update occurs,
the system wakes it up in the background to handle the update. If the
application starts this service and is then terminated, the system
relaunches the application automatically when a new location becomes
available. This service is available in iOS 4 and later, only on
devices that contain a cellular radio.
From https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
An app can be relaunched when the location changes. However, can it be started automatically when the phone is started? The documentation isn't quite clear.
The service will start when the user launches your application, and terminate if it is closed. The service will remain running if the application is running in the background.
Developers cannot integrate services into the OS, for security purposes.
No, you cannot have your application run automatically when the phone is started. In addition, if the user starts your application manually and puts it into the background, the system may eventually kill it when it needs the memory.
"Including the voip value in the UIBackgroundModes key lets the system know that it should allow the app to run in the background as needed to manage its network sockets. An app with this key is also relaunched in the background immediately after system boot to ensure that the VoIP services are always available."
check iOS docs here
Although if you do this for an illegitimate reason I am guessing your app will either not get approval or get booted quickly.
Related
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.
From the Core Location documentation:
The regions you register with the location manager persist between launches of
your application. If a region crossing occurs while your application is not
running, the system automatically wakes up your application (or relaunches it)
in the background so that it can process the event. When relaunched, all of
the regions you configured previously are made available in the
monitoredRegions property of any location manager objects you create.
Unfortunately, this is unclear as to whether or not the same app will also be relaunched after device reboot in response to a region change.
Will an iOS app that monitors significant location changes in the background still be relaunched, even after a device reboot?
Yes, your app will be launched to respond to the region events even if the phone is restarted, and even if the user did not explicitly run your app after the reboot.
I haven't seen any documentation that spells that out clearly. But this is how it works in a couple of my own apps.
Just to make an update. I know that this is an old question, but I want to confirm that it works on iOS7, even after a reboot.
Check my question here:
Are background mode location and fetch called even after the device restarted in iOS7?
Is there any way to start applicaiton when device restarts .
I mean - my appliaction is trying to get location when the application is running in background, but any how if user is restart the device then application will not provide any location updates.
So is there any way to keep application running even though the device is restarted ??
Thanks in advance.
No, iOS does not provide anything like an autostart functionality. There might be a way on jailbroken phones, but if you aim for the App Store you'll have to design your app/service that you can live with this limitation.
Edit: I stand corrected. If you set the voip value in the UIBackgroundModes key of your Info.plist, your app does get restarted after a device reboot. According to the documentation:
voip:
The application provides Voice-over-IP services. Applications with this key are automatically launched after system boot so that the application can reestablish VoIP services.
I'm working on a location tracking application that uses both the standard location service and significant change location service in the background (my app is registered for background location updates in iOS 4+) as applicable. Thanks to this question I have a solid understanding of how significant change comes back from the background state and relaunches from a terminated state. However, I'm still not clear on how the standard location service behaves in these instances. Could anyone break down the exact behavior of the standard location service running in the background?
Specifically:
How does the standard location service behave when you leave it running and the app suspends into the background? From my own testing, I've seen that it will continue waking up to receive callbacks on locationManager:didUpdateToLocation:fromLocation: (I have it send the location out a socket and I can see it on the network). Is there a time or memory limit for this callback to process?
Does the standard location service continue to run even when my app is terminated? That is, will it ever relaunch with application:didFinishLaunchingWithOptions: with UIApplicationLaunchOptionsLocationKey the way the significant change service does? I assume it the CLLocationManager would also need to be restarted in this case, as the significant change service does.
Thanks.
Answer to both 1 & 2, If you have registered your app as using background location then your app does not get terminated and continues to run in the background until you do this:
[locationManager stopUpdatingLocation];
So, there is no time or memory limit however there is a battery limit. If your battery is low, all apps using gps will be closed. Since your app is not terminated in normal circumstances, either it is not required to or it does not relaunch (after being terminated coz of battery) with UIApplicationLaunchOptionsLocationKey
Standard and significant services are different in this manner that significant wakes up the app whenevr there is a location update but standard makes app run continuously and hence drain battery.
Before you choose what service to use consider reading the Location-Awareness Programming guide.
I'm reading through the multitasking documentation, and it has a few references to apps which launch directly into the background state, never entering the foreground state. Is this really allowed for regular apps? Can anyone give me an example of an app like this?
VoIP apps are the biggest one to use this feature. Basically a VoIP app can register itself with the system to be notified when network traffic is intended for it at which point the app takes over handling the incoming traffic (i.e. receiving a call). Skype and Viber both use it.
From the iOS Developer Library (emphasis mine):
Including the voip value in the
UIBackgroundModes key lets the system
know that it should allow the
application to run in the background
as needed to manage its network
sockets. An application with this key
is also relaunched in the background
immediately after system boot to
ensure that the VoIP services are
always available.
The significant location changes backgrounding service also allows an app to be updated with the new location even if it's not running.
Other than those two cases, an app can't do anything from a terminated state until the user launches it.
When you use location manager with significantchange notification, App gets backgrounded automatically if the app is killed, when there is a location event