iOS locationManager: location update in my own interval with application in the background

If we run the LocationManager in the background, running all the time without a break. This method is especially demanding on the battery, and do not always need to detect location continuously. Often we need to know the location of the specified interval.

How to run an iOS application in the background?

iOS has several options how to correctly run the application in the background. These methods are enabled with iOS. We’ll use the one that your application will use and which we need. All services we can see in UIBackgroundModes array:

audio – The app plays audible content in the background.

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.

voip – The app provides Voice-over-IP services. Apps with this key are automatically launched after system boot so that the app can reestablish VoIP services. Apps with this key are also allowed to play background audio.

fetch – The app requires new content from the network on a regular basis. When it is convenient to do so, the system launches or resumes the app in the background and gives it a small amount of time to download any new content. This value is supported in iOS 7.0 and later.

remote-notification – The app uses remote notifications as a signal that there is new content available for download. When a remote notification arrives, the system launches or resumes the app in the background and gives it a small amount of time to download the new content.This value is supported in iOS 7.0 and later.

newsstand-content – The app processes content that was recently downloaded in the background using the Newsstand Kit framework, so that the content is ready when the user wants it.

external-accessory – The app communicates with an accessory that delivers data at regular intervals.

bluetooth-central – The app uses the CoreBluetooth framework to communicate with a Bluetooth accessory while in the background.

Service you want to use must allow the application. This we do in Info.plist and add value with key “Required backgdound modes“. We do it as follows:

How to run the standard locationManager in the background?

If we have included location service in background mode (set in info.plist), we can try run this service after switch application into background.

We will use the methods in the AppDelegate, which allows to recognize that the application was switched to background:

applicationDidEnterBackground: – Tells the delegate that the app is now in the background.

applicationWillEnterForeground: – Tells the delegate that the app is about to enter the foreground.

We can write a simple example that will run locationManager only if the application is in the background:

This example works so that if you switch the application into the background, starts location updating and when it switches back to the forefront, application stops updating location.

Tracking user location in intervals

In iOS natively we can’t change the interval at which the system updates the user location. IOS updates position regularly every second, if have GPS signal.

If the application is in the foreground, we could simply stop monitoring and again start it after interval, for example using NSTimer. In this case, we have to think about the life of the application. The application runs in the background and during idle stops working.

My final procedure is use NSTimer in the background by using UIApplication:beginBackgroundTaskWithExpirationHandler:. This timer triggers periodically while the application runs in the background. You can view the following example:

Well, for me it keeps running forever, and I’m not understanding why. In the Apple docs they say that we have 3 minutes while the app in the background and then it’s suspended, but with that code it keeps running (That’s what I want) .