Optimizing Device-Specific iOS Development
- Accessing Basic Device Information
- Adding Device Capability Restrictions
- Recipe: Checking Device Proximity and Battery States
- Recipe: Recovering Additional Device Information
- Recipe: Using Acceleration to Locate "Up"
- Working with Basic Orientation
- Retrieving the Current Accelerometer Angle Synchronously
- Recipe: Using Acceleration to Move Onscreen Objects
- Recipe: Accelerometer-Based Scroll View
- Recipe: Core Motion Basics
- Recipe: Retrieving and Using Device Attitude
- Detecting Shakes Using Motion Events
- Recipe: Using External Screens
- Tracking Users
- One More Thing: Checking for Available Disk Space
Read The Advanced iOS 6 Developer’s Cookbook, Fourth Edition or more than 24,000 other books and videos on Safari Books Online. Start a free trial today.
Each iOS device represents a meld of unique, shared, momentary, and persistent properties. These properties include the device’s current physical orientation, its model name, its battery state, and its access to onboard hardware. This chapter looks at the device from its build configuration to its active onboard sensors. It provides recipes that return a variety of information items about the unit in use. You read about testing for hardware prerequisites at runtime and specifying those prerequisites in the application’s Info.plist file. You discover how to solicit sensor feedback via Core Motion and subscribe to notifications to create callbacks when sensor states change. You read about adding screen mirroring and second-screen output, and about soliciting device-specific details for tracking. This chapter covers the hardware, file system, and sensors available on the iPhone device and helps you programmatically take advantage of those features.
Accessing Basic Device Information
The UIDevice class exposes key device-specific properties, including the iPhone, iPad, or iPod touch model being used, the device name, and the OS name and version. It’s a one-stop solution for pulling out certain system details. Each method is an instance method, which is called using the UIDevice singleton, via [UIDevice currentDevice].
The system information you can retrieve from UIDevice includes these items:
- systemName—This returns the name of the operating system currently in use. For current generations of iOS devices, there is only one OS that runs on the platform: iPhone OS. Apple has not yet updated this name to match the general iOS rebranding.
- systemVersion—This value lists the firmware version currently installed on the unit: for example, 4.3, 5.1.1, 6.0, and so on.
- model—The iPhone model returns a string that describes its platform—namely iPhone, iPad, and iPod touch. Should iOS be extended to new devices, additional strings will describe those models. localizedModel provides a localized version of this property.
- userInterfaceIdiom—This property represents the interface style used on the current device, namely either iPhone (for iPhone and iPod touch) or iPad. Other idioms may be introduced as Apple offers additional platform styles.
- name—This string presents the iPhone name assigned by the user in iTunes, such as “Joe’s iPhone” or “Binky.” This name is also used to create the local hostname for the device.
Here are a few examples of these properties in use:
UIDevice *device = [UIDevice currentDevice]; NSLog(@"System name: %@", device.systemName); NSLog(@"Model: %@", device.model); NSLog(@"Name: %@", device.name);
For current iOS releases, you can use the idiom check with a simple Boolean test. Here’s an example of how you might implement an iPad check. Notice the convenience macro. It tests for selector conformance and then returns [UIDevice currentDevice].userInterfaceIdiom if possible, and UIUserInterfaceIdiomPhone otherwise:
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
Should this test fail, you may currently assume that you’re working with an iPhone/iPod touch. If and when Apple releases a new family of devices, you’ll need to update your code accordingly for a more nuanced test.