iPhone OS Reference Library Apple Developer Connection spyglass button

UIDevice Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iPhone OS 2.0 and later.
Declared in
UIDevice.h
Related sample code

Overview

The UIDevice class provides a singleton instance representing the current device. From this instance you can obtain information about the device such as unique ID, assigned name, device model, and operating-system name and version.

You also use the UIDevice instance to detect changes in the device’s characteristics, such as physical orientation. You get the current orientation using the orientation property or receive change notifications by registering for the UIDeviceOrientationDidChangeNotification notification. Before using either of these techniques to get orientation data, you must enable data delivery using the beginGeneratingDeviceOrientationNotifications method. When you no longer need to track the device orientation, call the endGeneratingDeviceOrientationNotifications method to disable the delivery of notifications.

Similarly, you can use the UIDevice instance to obtain information and notifications about changes to the battery’s charge state (described by the batteryState property) and charge level (described by the batteryLevel property). The UIDevice instance also provides access to the proximity sensor state (described by the proximityState property). The proximity sensor detects whether the user is holding the device close to their face. Enable battery monitoring or proximity sensing only when you need it.

Tasks

Getting the Shared Device Instance

Identifying the Device and Operating System

Getting the Device Orientation

Getting the Device Battery State

Using the Proximity Sensor

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

batteryLevel

The battery charge level for the device.

@property (nonatomic,readonly) float batteryLevel
Discussion

Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.

If battery monitoring is not enabled, battery state is UIDeviceBatteryStateUnknown and the value of this property is –1.0.

Availability
See Also
Declared In
UIDevice.h

batteryMonitoringEnabled

A Boolean value indicating whether battery monitoring is enabled (YES) or not (NO).

@property (nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled
Discussion

Enable battery monitoring only when your application needs to be notified of changes to the battery state. Otherwise, disable battery monitoring. The default value is NO.

Availability
See Also
Declared In
UIDevice.h

batteryState

The battery state for the device.

@property (nonatomic,readonly) UIDeviceBatteryState batteryState
Discussion

The value for batteryState is one of the constants in “UIDeviceBatteryState.”

If battery monitoring is not enabled, the value of this property is UIDeviceBatteryStateUnknown.

Availability
See Also
Declared In
UIDevice.h

generatesDeviceOrientationNotifications

A Boolean value that indicates whether the receiver generates orientation notifications (YES) or not (NO). (read-only)

@property (nonatomic, readonly, getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications
Discussion

If the value of this property is YES, the shared UIDevice object posts a UIDeviceOrientationDidChangeNotification notification when the device changes orientation. If the value is NO, it generates no orientation notifications. Device orientation notifications can only be generated between calls to the beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications methods.

Availability
See Also
Declared In
UIDevice.h

localizedModel

The model of the device as a localized string. (read-only)

@property (nonatomic, readonly, retain) NSString *localizedModel
Discussion

This string would be a localized version of the string returned from model.

Availability
Declared In
UIDevice.h

model

The model of the device. (read-only)

@property (nonatomic, readonly, retain) NSString *model
Discussion

Possible examples of model strings are @”iPhone” and @”iPod touch”.

Availability
Declared In
UIDevice.h

name

The name identifying the device. (read-only)

@property (nonatomic, readonly, retain) NSString *name
Discussion

The value of this property is an arbitrary string that is associated with the device as an identifier. For example, you can find the name of an iPhone in the General > About settings.

Availability
See Also
Related Sample Code
Declared In
UIDevice.h

orientation

Returns the physical orientation of the device. (read-only)

@property (nonatomic, readonly) UIDeviceOrientation orientation
Discussion

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface. See “UIDeviceOrientation” for descriptions of the possible values.

The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.

Availability
See Also
Declared In
UIDevice.h

proximityMonitoringEnabled

A Boolean value indicating whether proximity monitoring is enabled (YES) or not (NO).

@property (nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled
Discussion

Enable proximity monitoring only when your application needs to be notified of changes to the proximity state. Otherwise, disable proximity monitoring. The default value is NO.

Not all iPhone OS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.

Availability
See Also
Declared In
UIDevice.h

proximityState

A Boolean value indicating whether the proximity sensor is close to the user (YES) or not (NO).

@property (nonatomic,readonly) BOOL proximityState
Availability
See Also
Declared In
UIDevice.h

systemName

The name of the operating system running on the device represented by the receiver. (read-only)

@property (nonatomic, readonly, retain) NSString *systemName
Availability
See Also
Declared In
UIDevice.h

systemVersion

The current version of the operating system. (read-only)

@property (nonatomic, readonly, retain) NSString *systemVersion
Discussion

An example of the system version is @”1.2”.

Availability
See Also
Related Sample Code
Declared In
UIDevice.h

uniqueIdentifier

A string unique to each device based on various hardware details. (read-only)

@property (nonatomic, readonly, retain) NSString *uniqueIdentifier
Discussion

A unique device identifier is a hash value composed from various hardware identifiers such as the device’s serial number. It is guaranteed to be unique for every device but cannot publically be tied to a user account. You can use it, for example, to store high scores for a game in a central server or to control access to registered products. The unique device identifier is sometimes referred to by its abbreviation UDID.

Availability
Related Sample Code
Declared In
UIDevice.h

Class Methods

currentDevice

Returns an object representing the current device.

+ (UIDevice *)currentDevice

Return Value

A singleton object that represents the current device.

Discussion

You access the properties of the returned UIDevice instance to obtain information about the device. You must instantiate the UIDevice instance before registering to receive device notifications.

Availability
Related Sample Code
Declared In
UIDevice.h

Instance Methods

beginGeneratingDeviceOrientationNotifications

Begins the generation of notifications of device orientation changes.

- (void)beginGeneratingDeviceOrientationNotifications

Discussion

You must call this method before attempting to get orientation data from the receiver. This method enables the device’s accelerometer hardware and begins the delivery of acceleration events to the receiver. The receiver subsequently uses these events to post UIDeviceOrientationDidChangeNotification notifications when the device orientation changes and to update the orientation property.

You may nest calls to this method safely, but you should always match each call with a corresponding call to the endGeneratingDeviceOrientationNotifications method.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UIDevice.h

endGeneratingDeviceOrientationNotifications

Ends the generation of notifications of device orientation changes.

- (void)endGeneratingDeviceOrientationNotifications

Discussion

This method stops the posting of UIDeviceOrientationDidChangeNotification notifications and notifies the system that it can power down the accelerometer hardware if it is not in use elsewhere. You call this method after a previous call to the beginGeneratingDeviceOrientationNotifications method.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UIDevice.h

Constants

UIDeviceBatteryState

The battery power state of the device.

typedef enum {
   UIDeviceBatteryStateUnknown,
   UIDeviceBatteryStateUnplugged,
   UIDeviceBatteryStateCharging,
   UIDeviceBatteryStateFull,
} UIDeviceBatteryState;
Constants
UIDeviceBatteryStateUnknown

The battery state for the device cannot be determined.

Available in iPhone OS 3.0 and later.

Declared in UIDevice.h.

UIDeviceBatteryStateUnplugged

The device is not plugged into power; the battery is discharging.

Available in iPhone OS 3.0 and later.

Declared in UIDevice.h.

UIDeviceBatteryStateCharging

The device is plugged into power and the battery is less than 100% charged.

Available in iPhone OS 3.0 and later.

Declared in UIDevice.h.

UIDeviceBatteryStateFull

The device is plugged into power and the battery is 100% charged.

Available in iPhone OS 3.0 and later.

Declared in UIDevice.h.

Discussion

These constants are used by the batteryState property.

UIDeviceOrientation

The physical orientation of the device.

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;
Constants
UIDeviceOrientationUnknown

The orientation of the device cannot be determined.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationPortrait

The device is in portrait mode, with the device held upright and the home button at the bottom.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationPortraitUpsideDown

The device is in portrait mode but upside down, with the device held upright and the home button at the top.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationLandscapeLeft

The device is in landscape mode, with the device held upright and the home button on the right side.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationLandscapeRight

The device is in landscape mode, with the device held upright and the home button on the left side.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationFaceUp

The device is held perpendicular to the ground with the screen facing upwards.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

UIDeviceOrientationFaceDown

The device is held perpendicular to the ground with the screen facing downwards.

Available in iPhone OS 2.0 and later.

Declared in UIDevice.h.

Discussion

The orientation property uses these constants to identify the device orientation. These constants identify the physical orientation of the device and are not tied to the orientation of your application’s user interface.

Notifications

All UIDevice notifications are posted by the singleton device instance returned by currentDevice.

UIDeviceBatteryLevelDidChangeNotification

Posted when the battery level changes.

Notifications for battery level change are sent no more frequently than once per minute. Do not attempt to calculate battery drainage rate or battery time remaining; drainage rate can change frequently depending on built-in applications as well as your application.

You can obtain the battery level by getting the value of the batteryLevel property.

Availability
Declared In
UIDevice.h

UIDeviceBatteryStateDidChangeNotification

Posted when battery state changes.

You can obtain the battery state by getting the value of the batteryState property.

Availability
Declared In
UIDevice.h

UIDeviceOrientationDidChangeNotification

Posted when the orientation of the device changes.

You can obtain the new orientation by getting the value of the orientation property.

Availability
Declared In
UIDevice.h

UIDeviceProximityStateDidChangeNotification

Posted when the state of the proximity sensor changes.

You can obtain the proximity state by getting the value of the proximityState property.

Availability
Declared In
UIDevice.h


Last updated: 2009-04-28

Did this document help you? Yes It's good, but... Not helpful...