MOBILE

iPhone Programming : Using Sensors - Hardware Support

12/20/2012 3:36:12 AM

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 featuresOriginal iPhoneiPhone 3GiPhone 3GSFirst-generation iPod touchSecond-generation iPod touchThird-generation iPod touch
Cellularxxx   
WiFixxxxxx
Bluetoothxxx xx
Speakerxxx xx
Audio-inxxx xx
Accelerometerxxxxxx
Magnetometer  x   
GPS xx   
Proximity sensorxxx   
Cameraxxx   
Video capture  x   
Vibrationxxx   

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 AddExisting 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 ProjectRename 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.

Figure 1. Setting the “Required device capabilities” key


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.

Figure 2. Adding the location-services item to “Required device capabilities”

Other  
 
Most View
Belkin AC1200 DB Wi-Fi ADSL Router
Ditch Your Laptop For Your Phone (Part 1)
Installing and Configuring SharePoint 2013 : Creating the Farm (part 1)
Using Services for UNIX to Integrate UNIX Systems with an Active Directory/Exchange Server 2007 Environment
Apple - Celebrating 7 Years Of Success
ASP.NET 4 : Error Handling, Logging, and Tracing - Handling Exceptions
Security Pros Get Caught Out By QR Codes
Samsung ATIV Tab Review - A Wonderful Windows RT Tablet (Part 2)
How To Extend Life For Mac (Part 1)
Edifier E10 Exclaim - Exclamation Mark
Top 10
Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Access Services
Sharepoint 2013 : Developing Applications Using Office Services - The New Machine Translation Services
Sharepoint 2013 : Developing Applications Using Office Services - Word Automation Services and the New PowerPoint Automation Services
Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Excel Services
Sharepoint 2013 : Developing Applications Using Office Services - WOPI and the New Office Web Apps Server
Sharepoint 2013 : Building a BCS-enabled Business Solution : Building an Integrated BCS Solution with an App for SharePoint Containing an App for Office
Business Connectivity Services in Apps for SharePoint 2013 : Building an App-level BCS Solution for Office 365 SharePoint Online
Business Connectivity Services in SharePoint 2013 : Adding a Business Data Connectivity Model to Office 365 SharePoint Online
Remote Event Receivers in Sharepoint 2013 : Introducing Remote Event Receivers
Windows Server 2008 and Windows Vista : Common GPO Troubleshooting Tools (part 3) - GPResult, GPOTool