…continuing on from the last post, I present you the final instalment of extra notes to my video2brain video training “iPhone and iOS Application Development Workshop” (see course contents): This time I’m going to cover how you can access compass data…
Obtaining Heading information from a device’s compass
If your user’s device has a compass then you’ll also be able to obtain heading data and determine the direction in which their device is pointing. This will work independent of whether or not your App has Core Location Services running and active.
If Core Location Services are active and accessible for your App (i.e. the user has granted your App permission to access their location) then you’ll receive both magnetic headings and true headings data – with the true heading having being calculated based upon the device’s location and taking magnetic declination (sometimes called magnetic variation) into account!
Without Core Location Services running for your App you’ll only receive magnetic heading data, making headings used by your App less accurate.
Adding heading information to your App is simply a matter of setting up and initialising your Location Manager to receive heading updates:
The code below shows compass support added to the myLocation App including the required delegate method, used to receive updates to heading data:
When accessing a device’s compass for the first time, a calibration display will be shown:
To remove this display from the screen after a short amount of time, allowing you to maintain the integrity of your visual design, use the CLLocationManager dismissHeadingCalibrationDisplay method.
An important thing to note when using headings data is that you MUST check for device support using the headingAvailable method of CLLocationManager. If the device does not have a compass and you start monitoring for heading-related updates, your delegate methods will receive no events at all.
Also, if your App stops running, all heading-related updates will also stop. You will then need to start receiving updates again upon restart of your App. If your App continues to run in the background, updates will be suspended and then resumed automatically once your App is awakened. This is an important difference in the treatment of heading data updates from location data updates.
Developing location-aware Apps – over to you!
You’ve now seen how to implement location-based features in your Apps and also learned more about the important issues of battery life. There are practically limitless opportunities out there for good location-aware Apps and the very nature of mobile devices such as iPhones (and iPads) makes this type of App not only a logical, but also an often very practical, choice.
The only limit to what you can do with a user’s location and heading information lies in how we choose to use it. Combined with other device capabilities, we have powerful tools at our disposal for the creation of exciting and entertaining Apps!
comments are closed.