…continuing on from the last post, the third of five extra notes to my video2brain video training “iPhone and iOS Application Development Workshop” will cover location change monitoring and updates in a little more detail:
Receiving Location Updates
Now you’ve made sure that Core Location Services are enabled, device support for your update scheme is present and your user has granted permission. Now just how much of that location data do you need, and when?
Do you need to deliver a constant stream of up-to-date and accurate location data or is it enough to simply detect a change in the user’s location every kilometer or so. Equally, you may not need to constantly receive updates and it may be fine for your App to run in the background and trigger an update only when a user enters a certain region – by using region-based location change monitoring.
In the myLocation example we used standard monitoring to receive location updates. Standard monitoring is the most logical and suitable choice for Apps which are intended to be used actively while tracking location, as opposed to Apps which will run in the background and serve to provide some form of location-based alert. Examples of such Apps are route navigation and interactive “where am I?” type Apps. These Apps are normally used only for a certain period of time as the potential for battery drain using standard monitoring is high unless settings are used to limit this.
We didn’t specify a desired accuracy or distance filter in myLocation but these are two settings that you certainly would want to set in practice.
Desired accuracy is possibly the most important setting to consider when designing and implementing your App as it’s the setting that will have the most potential impact on power consumption – one of the main issues associated with location-aware Apps.
Distance filters are used to define the amount of distance a device must be moved before a location update is triggered. This will also have an effect on battery life and you should, therefore, ensure that you only trigger updates as you need them.
We’ll take a closer look at settings for accuracy and distance filters in a few moments. First, a few words about region-based monitoring:
Region-based monitoring is going to be easier on your user’s device batteries and is ideal for implementing location-based reminders or alerts, for example:
So, how do you set up your App to monitor for, and react to, region-based location changes?
Once you’ve confirmed that a device can monitor for Region changes your App will need to define Regions to be monitored for and then react to updates from the Location Manager. Add a region to monitor for by creating a CLRegion Object:
(The desired accuracy of 100.0 will ensure that an update is triggered once the device is within 100m of the region. Reduce this for more accuracy)
Then using the CLLocationManager delegate methods didEnterRegion and didExitRegion to start monitoring for entry into, and exit from that region:
You’ll probably want to save a reference to regions being monitored within your App and then display an Alert or information relating to that region, or react to entry into the region in the didEnterRegion method.
One important thing to consider about using region-based monitoring is that your App will receive location updates should your user move into a defined region while the App is not running. This means that you’ll need to be able to react to your App then being restarted or “woken up” by the system and ensure that you have the correct code in place in your App Delegate to then present the required alert to your user (or provide other functionality as required):
Significant location change monitoring:
Significant Location change monitoring is an excellent way to react to changes in location which involve movement from one cell tower to another as, as such, to reduce power consumption and avoid too-frequent updates. Using this update scheme will result in an initial location fix being carried out upon initialization of the Location Manager and then for updates to be delivered only upon a significant location change.
As with region-based monitoring, significant location change monitoring will remain in effect when your App is closed down and may result in your App being restarted. Ensure that you react to these events in your App delegate as required by your App’s design:
Making the right choice
With the various location update schemes available, it’s important to consider which type of updates your App will need.
Another important point to remember is that you’ll increase your chances of having happy App users if you describe how your App uses location data in your App Store description – and address any concerns users may have about the amount of battery drain they may experience when using your App:
If possible, provide a means for your App users to choose a less intensive “power-saving” option and use this as an important selling point for your App. Battery life is always a potential point of contention with location-aware Apps and making a feature of your power-saving functionality may well be a deal-clincher for more hesitant buyers.
Note: These notes were compiled at the time of production of the “iPhone and iOS Application Development Workshop” course and there have since been some exciting updates to the Core Location API. Watch this space for some more iOS 5 updates – coming soon!
If you haven’t seen the course yet, then take the opportunity to purchase it today for only £21.99 – that’s 3 hours 45 minutes of video training (a lot of coding!) for next to nothing. Here’s the link:
Meanwhile, the fourth and then final part of these extra notes will be appearing here in a few days…
Until then, have a good weekend!
comments are closed.