The arrival of the iPhone changed the whole direction of software development for mobile platforms, and has had a profound impact on the hardware design of the smartphones that have followed it. The arrival of the iPad has turned what was a single class of devices into a platform.
That platform is one of the most popular for geolocation: it’s used for everything from driving directions to finding a restaurant. As a developer, you can get in on the geolocation game by using the Core Location framework, one of the most powerful and interesting frameworks in the iOS SDK. It abstracts the details of determining a user’s location, and does all the heavy lifting for you behind the scenes.
Unique amongst modern mobile platforms, Apple has gone to great lengths to ensure that your code will run on all of the current iOS-based devices. Yet despite this, there is still some variation in hardware between the various models (see Table 1-1).
Table 1-1. Hardware support in various iPhone, iPod touch, and iPad models
Hardware features | iPhone | iPod touch | iPad | iPad 2 | New iPad | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Original | 3G | 3GS | 4 | 4s | 1st gen | 2nd gen | 3rd gen | 4th gen | WiFi | 3G | WiFi | 3G | WiFi | 3G | |
Cellular | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
WiFi | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Bluetooth | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Speaker | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Audio In | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Accelerometer | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Magnetometer | ✗ | ✗ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Gyroscope | ✗ | ✗ | ✗ | ✓ | ✓ | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
GPS | ✗ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✗ | ✓ | ✗ | ✓ |
Proximity Sensor | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ |
Camera | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
Video | ✗ | ✗ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
Vibration | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ |
Retina Display | ✗ | ✗ | ✗ | ✓ | ✓ | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ |
Because your application will likely support multiple devices, you’ll need to write code to check which features are supported and adjust your application’s behavior as appropriate.
The short answer to a fairly commonly asked question is that, unfortunately, the Core Location framework does not provide any way to get direct information about the availability of specific hardware such as the GPS at application run time, although you can check whether location services are enabled:
BOOL
locationAvailable
=
[
CLLocationManager
locationServicesEnabled
];
However, while you cannot check for the availability of GPS using Core Location from your application, you can require the presence of GPS hardware for your application to load (see Setting Required Hardware Capabilities).
If your application requires specific hardware features in order to run you can add a list of required capabilities to your application’s Info.plist file. Your application will not start unless those capabilities are present on the device.
To do this, open your project and then click your application’s Info.plist file to open it in the Xcode editor. Click the last entry in the list and a + button will appear on the right side of the key-value pair table.
Click this button to add a new row to the table, and scroll down the
list of possible options and select “Required device capabilities” (the
UIRequiredDeviceCapabilities
key). This
will add an (empty) array to the plist file.
The allowed values for the keys are:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A full description of the possible keys is given in the Device Support section of the iOS Application Programming Guide available from the iOS Development Center.
Setting the UIBackgroundModes
key in the Application’s Info.plist
file notifies the operating systems that the application should continue
to run in the background, even after the user closes it, since it
provides specific background services.
Note
Regarding background modes, Apple says: “These keys should be used sparingly and only by applications providing the indicated services. Where alternatives for running in the background exist, those alternatives should be used instead. For example, applications can use the significant location change interface to receive location events instead of registering as a background location application.”
There are three possible key values: audio, location, and voip. In this book, you’ll be most interested in the location key, which indicates that the application provides location-based information for the user using the standard Core Location services, rather than the newer significant location change service, and should continue to do so in the background (see Chapter 2).
Get Geolocation in iOS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.