Chapter 29. Discovering Information about the Device
In This Chapter
How to tell if the device is connected to a power adapter |
Working with the proximity sensor |
Getting basic information and metadata about the device |
Cocoa Touch provides a rich API for working with the iPhone hardware. The most basic capabilities of that API are based around the UIDevice
class.
UIDevice
is a singleton that allows basic interaction with some of the core elements of the iPhone hardware. It enables you to find out information about the device orientation, the battery level, and the proximity sensor. It can also tell you things such as what type of device it is, as well as the unique identifier of the device. It's an incredibly powerful little gem in Cocoa Touch, one that can be exploited easily in your applications.
Accessing the Battery State
The first item that I am going to discuss with regard to device hardware knowledge is information about the battery level. Using the UIDevice
singleton, you can monitor both the battery charge level and the current battery state. For example, you can find out whether or not the battery is currently being charged from a power adapter.
To utilize this capability, you simply instantiate the UI device singleton, enable battery monitoring, and then retrieve the battery level using the batteryLevel
property. The return value from this property ranges from 0.0, indicating a fully discharged battery, to 1.0, indicating a fully charged battery. Additionally, the batteryState ...
Get Cocoa Touch™ for iPhone® OS 3 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.