While the iPhone is almost unique among mobile platforms
in guaranteeing that your code will run on all of the current
devices, there is some variation in available hardware between the various
models.
1. Determining Available Hardware Support
Table 1 lists
the hardware differences between the devices. Because your app 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.
Table 1. Hardware support in various iPhone and iPod touch
models
Hardware features | Original iPhone | iPhone 3G | iPhone 3GS | First-generation
iPod touch | Second-generation
iPod touch | Third-generation
iPod touch |
---|
Cellular | x | x | x | | | |
WiFi | x | x | x | x | x | x |
Bluetooth | x | x | x | | x | x |
Speaker | x | x | x | | x | x |
Audio-in | x | x | x | | x | x |
Accelerometer | x | x | x | x | x | x |
Magnetometer | | | x | | | |
GPS | | x | x | | | |
Proximity sensor | x | x | x | | | |
Camera | x | x | x | | | |
Video capture | | | x | | | |
Vibration | x | x | x | | | |
1.1. Network availability
We can easily determine
whether the network is reachable, and whether we are using the
wireless or WWAN interface:
Reachability *reach = [
[Reachability reachabilityForInternetConnection] retain];
NetworkStatus status = [reach currentReachabilityStatus];
1.2. Camera availability
However, it
is simple to determine whether a camera is present in the
device:
BOOL available = [UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
Once you have determined that a camera is present, you can
inquire whether it supports video by making a call to determine the
available media types the camera supports:
NSArray *media = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
If the kUTTypeMovie media
type is returned as part of the array, the camera will support video
recording.
1.3. Audio input availability
You can poll whether audio input is available using the AVAudioSession singleton class by checking
the inputIsAvailable class
property:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL audioAvailable = audioSession.inputIsAvailable;
Note:
You will need to add the
AVFoundation.Framework (right-click or Ctrl-click on the Frameworks folder in
Xcode and choose Add→Existing
Frameworks). You’ll also need to import the header (put this in your
declaration if you plan to implement the AVAudioSessionDelegate
protocol, discussed shortly):
#import <AVFoundation/AVFoundation.h>
You can also be notified of any changes in the availability of
audio input (e.g., a second-generation iPod touch user has plugged in
headphones with microphone capabilities). First, nominate your class
as a delegate:
audioSession.delegate = self;
Declare it as implementing the AVAudioSessionDelegate protocol in the
declaration:
@interface YourAppDelegate : NSObject <UIApplicationDelegate,
AVAudioSessionDelegate >
Then implement inputIsAvailableChanged:
in the implementation:
- (void)inputIsAvailableChanged:(BOOL)audioAvailable {
NSLog(@"Audio availability has changed");
}
1.4. GPS availability
I’m going to cover the Core Location framework, and GPS. However, 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.
While you cannot check for the availability of GPS using Core
Location, you can require the presence of GPS hardware for your
application to load. I will discuss this in the next
section.
2. 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.
Note:
You may want to make a copy of the Weather application before
modifying, as we have done previously. Navigate to where you saved the
project and make a copy of the project folder, and then rename it.
Then open the new (duplicate)
project inside Xcode and use the Project→Rename tool to rename the project.
Open the Weather application in Xcode, open the
Weather-Info.plist file in the Xcode editor, and
click on the bottommost entry. A button with a plus sign (+) on it will
appear to the righthand side of the key-value pair table. Click on this
button to add a new row to the table; then scroll down the list of
possible options and select “Required device capabilities” (the
UIRequiredDeviceCapabilities key) as shown
Figure 1. This will
add an (empty) array to the .plist file. If you add
“location-services” (see Figure 2) as Item 0 of this
array (some versions of Xcode may label the first item in the array Item
1), your application will no longer start if such services are
unavailable. If you want to add further entries, select Item 0 and click
the plus button to the righthand side of the table.
The allowed values for the keys are
telephony, sms,
still-camera,
auto-focus-camera,
video-camera, wifi,
accelerometer,
location-services, gps,
magnetometer, microphone,
opengles-1, opengles-2,
armv6, armv7, and
peer-peer. A full description of the possible keys
is available in the Device Support section of the iPhone Application
Programming Guide available from the iPhone Dev Center.