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

This is a long awaited article that I promised to write. A couple months ago, I found a way to get the location update from iOS 7 and 8 even when the app is not active on foreground and not in the background. Yes, you may get the GPS coordinates from the iOS devices even when the app is killed/terminated either by the user or the iOS itself.

Apple does not communicate much with the developers on the ways to do that. If you remember this post from early 2014: Apple’s iOS 7.1 will fix a geolocation bug — after developer sends letter to Tim Cook. In iOS 7.0, Apple prevents the location update when the app is killed/terminated/suspended. But, Apple has fixed the “bug” starting from iOS 7.1 by allowing the app to get the location coordinates.

I have read the release notes for iOS 7.1 but I couldn’t find any information related to location updates and also the way to get the location when the mobile application is terminated/killed/suspended. The iOS 7.0 to iOS 7.1 API Differences for CoreLocation is stated as “No changes“. What?!

But, I didn’t give up in finding the solution. So for a few months, I used my free time to play a bit of the iOS APIs including the CoreLocation APIs. After many months of trials and errors, I finally found the solution.

I will provide the full solution, upload to the Github and write the detailed information on this post.

How to get Location Update for iOS 7 and 8 even When the App is Killed/Terminated

I have written an old article related to getting the continuous Background Location Updates for iOS 7 and iOS 8.

The method to get the location in the background continuously for iOS 7 and 8 is using the method “startUpdatingLocation

and then the next trick would be on the delegate method “didUpdateLocations“. You will have to use a timer and handle the Background Task appropriately. Any missing steps and the location will not be updated continuously.

But in the case of getting the location when the app is terminated/suspended, I can not use [myLocationManager startUpdatingLocation]; The only way to make it work is to use:-

Surprised. Surprised. Yes, I only figured that out after many trials and errors.

Another important trick is, you will have to know how to handle the key “UIApplicationLaunchOptionsLocationKey” on the app delegate “didFinishLaunchingWithOptions“. Here is the sample code:-

In addition to the didFinishLaunchingWithOptions method, I have created a locationManager Instance when the app is active. Here are some code examples:

What exactly is the LaunchOptions Key “UIApplicationLaunchOptionsLocationKey”?

UIApplicationLaunchOptionsLocationKey is very important to get the location update even when the app is neither active nor in the background.

If you read its description from the XCode, it says: “The presence of this key indicates that the app was launched in response to an incoming location event”. What does it mean?

You may realize that when your app is launched for the First time, the App Delegate method “didFinishLaunchingWithOptions” will run once. If you put your app into the background and then launch the app again from the background. This app delegate method will not run again.

The method will only run again after you killed/terminated the app by swiping up from the App Preview Screen and then relaunch from where your app is located.

If your app is a location based mobile application and you need the user’s location even the app is terminated/suspended, then there is another way to launch the app without the user interaction. The iOS will launch the app for you.

When your app delegate methods LaunchOptions contains the key UIApplicationLaunchOptionsLocationKey, it means that your app is launched by the iOS because there is a significant location changes. So, you will have to do something with the new location GPS coordinates return from the location manager.

If order to do so, you will have to create a new instance of location manager and you shall get the location coordinates from the method “didUpdateLocations“.

How Frequent is the Location Updates?

As per Developer Documentation: Core Location Documentation.

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

So, you could only expect the location update if the device moves over 500 meters and at most once in every 5 minutes.

From my own testing (I am driving around a lot! To test the Core Locaton API), I only get the location update about every 10 minutes.

Full Source Code for the Location Updates Even when the iOS mobile apps is Suspended/Terminated/Killed

In this source code, I have included the methods to save the location GPS coordinates into a PList file. The purpose of saving into a PList is to record the location coordinates, the iOS Application States and the way locationManager is created (Add From Resume or otherwise).

Here are some simple instructions on how to test this solution:-

1. Download the project from Git Hub.
2. Change to your own Bundle Identifier (If need to)
3. Connect your iPhone with your mac.
4. Launch the app into your iPhone.
5. Killed the app. (Double tap and remove the app from the App Preview)
6. Travel around with your iPhone for a few days.
7. Download iFunBox for Mac or any alternative that allows you to open the app and Read the PList from the “Documents” folder.
8. From the PLIst, you will see something like the screen shot below:-
Getting Location Even When the App is Suspended/Killed/Terminated

When you see the key “UIApplicationLaunchOptionsLocationKey” under “Resume“, it means that the iOS has relaunched the app after the devices has moving significantly from the last known location. If you create a new location Manager instance, you will get new location from delegate method “didUpdateLocations“.

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

iOS Region Monitoring and Location Manager

One interesting feature in the new version of Watch Over Me app is Crime Zone. The main purpose of the crime zones is to notify the user of Watch Over me when he/she is passing the area. Watch Over Me app will ask if the user wants to be watched over during that time.

We have accumulated some trustable and reliable resources from various websites from the Internet to gather the crime zone areas in 7 major cities in 5 different countries (Malaysia, USA, UK, Australia, Thailand). These 7 cities are our test regions and we will be adding more regions in the future.

iOS Region Monitoring and Location Manager

I am using Region Monitoring from the Location Manager to implement this feature on the iOS Watch Over Me app. This particular module is not hard compare with getting the location in iOS 7 in background mode.

The maximum regions that an iOS device can monitor is only limited to 20. So, I have to come out with a good strategy to only get and monitor the regions that are useful to the users.

This is the logic we use for the region monitoring in iOS:-
1. Getting the current location from the user.
2. Send the current location to the server.
3. The server will return all the regions within a 20 km radius from the current location
4. If there are more than 20 regions, the server will only send the closest 20 regions from the current location.
5. After receiving the regions, the app will start to monitor the regions.

A Potential Bug for locationManager delegate method didEnterRegion

After the app has been released on the app store, we are still constantly in testing on the region monitoring on Kuala Lumpur (KL). We have 8 monitored regions in KL at the time of typing this article.

On one fine day, my colleague saw a bug with multiple region notifications triggered at the same time. The location of my colleague is at least 3000 meters away from the nearest region that we monitored. I have double checked on the radius and also the coordinate for all the regions that we monitor in KL, there wasn’t any discrepancy of the data.

Region Monitoring Bug
iOS Region Monitoring Bug
Region MonitoringiOS Region Monitoring

The screen on the Left shows the bug when 4 notifications triggered at the same time when the device is NOT even close to any of the regions.

The screen on the Right is a simple app that I made to stimulate the location of the device (Green Marker) and also the location (Red Marker) and the radius of each region (Red Circle). The Blue Circle is the estimation of the radius of the device when it triggered the notification for all 4 regions. The estimated radius for the device is about 4700 meters! It should not happen.

I have searched through various websites (StackOverFlow, Apple Developer Forum and etc.), I couldn’t find other developer faced the same issue like mine. I saw a lot of questions on NOT getting any notification when passing through a region, but my case is the entire opposite.

I am not sure if this is the fault on my code, on iOS system level or the problem on the local Telco in my country. I have posted a question on StackOverFlow for more than 1 week but I didn’t get any answer as well.

How to Fix this Bug?

Without much reliable information, I can only do trial and error in order to fix this bug. I have been trying a few different method on trying to fix the bug. This includes setting the distanceFilter = 10 for the locationManager. Previously, it was kCLDistanceFilterNone because I want the app to detect every single movement.

My colleague still saw this bug after a few days of testing. So, the above solution is no good.

After experimenting for sometimes, I found a solution that works good.

1. Whenever a user enters a monitored region, he/she will get a notification in the locationManager delegate method didEnterRegion. But due to the bug, this method is not reliable. I have to use a smart way to filter out the fake location.
2. I found a way to get the recent location of the user from locationManager.
3. After the app gets the user location, the app will make sure that the location is within the region before deciding to send the location notification to the device.

From my research, this location is not 100% reliable, especially if there isn’t any location based running at the background. But generally, it is good enough to filter out the bad location which may trigger multiple notifications for different regions.

Here are the example code:

iBeacon and Indoor Positioning System (IPS)

I have been excited for the Indoor Positioning System (IPS) Technology for a quite while. In fact, one of the main reasons I train myself to be an iOS developer is because I want to develop some excited Location and Positioning Based Mobile apps that will change the way we do things in life.

What is Indoor Positioning System?

Indoor Positioning System is a new technology that probably only exists in less than 15 years ago. When I am trying to find more information about this IPS technology from Wikipedia, the earliest research paper on the reference was dated based in year 2009. But when I am searching through google search, I found some research papers on early 2000s.

So, this is a relatively new technology that we haven’t see much applications on the real world yet. But, this will definitely change in the next 5 years.

With the exponential growth of the smart phone users, we have seen a lot of location based (GPS) mobile applications that pop up in the last 5 years. For your information, Global Positioning System (GPS) was developed in 1940s during the World War 2. So, GPS was initially developed to pinpoint the enemy’s location so that they can launch a missile to that location. Thanks to the mobile technology, the GPS technology has been highly commercialised to the mass market in the last 5 years.

All the smart phones users know very well that the Global Positioning System (GPS) does not work well inside a building. This is where the Indoor Positioning System comes into existence.

I am actually betting that the Indoor Positioning System will become highly commercialised within the next 5 years. There will be a lot of innovative mobile apps coming out of it that will make our life easier.

What is iBeacon?

Beacon is actually a device that transmits signal to its surrounding to determine the approximate distance betweens some objects and the broadcaster. We have seen some real world applications like on a car with reverse sensor. That sensor is actually a type of beacon.

iBeacon, on the other way, is a term trademarked by Apple. It is a new iOS technology which is only available on iOS 7 and onwards. iBeacon is using Bluetooth LE (Low Energy) to determine the your location as far as 10 50 meters inside a building.

The vivid location based Mobile apps users know very well that the location based apps usually consume a lot of battery, especially on the navigation mobile apps.

One of the big advantages over the GPS technology is that the IPS technology such as Bluetooth consumes a lot less battery. Even if you turn your bluetooth on 24 hours per day, you almost never feel any battery drain at all.

Some people mentioned that iBeacon is better than the Near Field Communication (NFC) technology that is available on some Android devices. It is due to its greater distance in discovery (iBeacon: 10 50 meters vs. NFC: 20 cms) and energy efficiency.

You may learn the details on the iBeacon here: iBeacon – Wikipedia.

So, What’s Next?

Some people mentioned that after Steve jobs passed aways, there isn’t much innovations coming out from Apple. This isn’t true. We have seen the innovation from Apple from iPhone 5s which has the fingerprint recognition technology and also this iBeacon Technology.

The DNA that Steve Jobs has implemented into Apple will last for a very long time. Apple was late in the map and location technology when comparing with Google. But, it has been catching up fast.

Apple has acquired an Indoor Positioning company – WiFiSlam for $20 millions. There isn’t much news from Apple after the acquisition. I believe we will hear something within the next 1 year.

Don Dodge, who is a Developer Advocate for Google, mentioned in his blog post that there will be many potential markets that could adopt IPS technology. There will be a lot of winners in this technology. He is also an investor and also advisor for some of the IPS startups.

I am very excited to see what will be coming out from iBeacon and Indoor Positioning System within the next 5 years. I just started to learn iBeacon iOS framework and I will make a new post when I am ready to share.