Archives for January 2014

Background Location Update Programming for iOS 7 and iOS 8

Before iOS 7, I used some location update solutions found from stackoverflow. After the iOS 7 is released, the location update (especially on background) is not working well in iOS 7. There are many existing location based apps had the same problem. It was due to the background services in iOS 7 which kill the app when the app is in suspended state.

After a few weeks of trial and error in experimenting many different solutions, I finally found one solution that works well for the app that I need to develop.

Note: The solution might not be elegant. It works well for what I want to create. When I am free, I am still constantly looking for more elegant solutions by exploring the background services in iOS 7.

iOS Application States and Multitasking

Before I dig deeper into the location services, I would like to share a bit on the iOS app states. It is very important for iOS 7 and above because we have multitasking background services starting from this version.

iOS App States

 

There are basically 5 states:-
1. Inactive – The app is running on foreground but there is no activity at the moment.
2. Active – The app is running on foreground and there are some activities.
3. Background – The app is on the background and still executing code. The app can move to suspended mode anytime now. But, the app can request extra time in executing code for a short period of time.
4. Suspended – The app is on the background and not executing code. Normally, the system will move app from background mode to this state automatically without any notification. But the app is still in memory.
5. Not running – The app is either not running or has been terminated by the system during low memory condition.

Most of the solutions before iOS 7 work well because during that time the system does not have multitasking and the system does not automatically move the app from background mode to suspended mode.

So, if your app needs consistent location update from the device, you will have to implement a solution that constantly refresh the app when it is in background mode so that it has a short period of time in executing the code and send the location to your server.

For more information on App States and Multitasking, you can visit: Guidelines by Apple on App States and Multitasking.

Background Location Update Programming for iOS 7

Behaviour of the location service that the app I have to develop:-
1. During the normal time, the app has to send the location to the server every 3 minutes.
2. When the user is running event, the app has to send location to the server every 1 minute.
3. The location has work both in foreground and background.

So, this app will get the location from the phone all the time. I have tested many solutions and the solution below works exactly as I needed and it consumes about 5% of battery per hour.

First of all, if you are using storyboard, you will have to make sure that “location update” under the Background Modes is enable. Screen shot as below:-
iOS 7 Location Background Mode This is very important as without that your app will not be able to get location when it is in background.

I created a class just to handle the location service. The most important function from the locationManager delegate is didUpdateLocations. You will need to know how to handle this function properly. I have programmed to make the locationManager restarts every 1 minute. When the locationManager is started, it will only last for 10 seconds to receive locations update from the device. So basically, the location Manager is only turned on for 10 seconds for 60 seconds.

I have tested on my device and it only consumes about 5-6% of battery every hour. The most important function is listed below with some explanation.

Updated on February 2014: Added the solution on Github and post to the thread in stackoverflow.

Updated on April 2014: EC has found a fix for the location update for an extended period of time.

Updated on May 2014: I received some requests by some developers (through email contact and the comment area of this post) on the sample codes for sending the locations to server on a certain time interval. I have added the sample codes and I have integrated EC’s fix on BackgroundTaskManager.m too. Full Solution: Background Location Update for iOS 7 with Location Update to Server.

Updated on September 2014: I have updated the solution to work in iOS 8. A new key NSLocationAlwaysUsageDescription has been added to the PList. It is a requirement in iOS 8 to inform the users on the reasons you need the location update from their devices. [locationManager requestAlwaysAuthorization]; has to be called in iOS 8 as well when getting the location.

Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended

I have promised to write an article related to getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended. I have had a busy development schedule in the last few months until the New Year Holiday break. Finally, I have time to write the articles and post the solution on Github.

Please read this article for more details: Getting Location Updates for iOS 7 and 8 when the App is Suspended.