iOS Developer Library

Developer

UIKit Framework Reference UIDevice Class Reference

Options
Deployment Target:

On This Page
Language:

UIDevice

Inheritance


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.

The UIDevice class provides a singleton instance representing the current device. From this instance you can obtain information about the device such as 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.

Starting in iOS 4.2, you can use the playInputClick instance method to play keyboard input clicks in custom input and keyboard accessory views.

  • Returns an object representing the current device.

    Declaration

    Swift

    class func currentDevice() -> UIDevice

    Objective-C

    + (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether multitasking is supported on the current device. (read-only)

    Declaration

    Swift

    var multitaskingSupported: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isMultitaskingSupported) BOOL multitaskingSupported

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 4.0 and later.

  • name name Property

    The name identifying the device. (read-only)

    Declaration

    Swift

    var name: String { get }

    Objective-C

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

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

    See Also

    systemName

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

    Declaration

    Swift

    var systemName: String { get }

    Objective-C

    @property(nonatomic, readonly, retain) NSString *systemName

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    var systemVersion: String { get }

    Objective-C

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

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

    See Also

    systemName

  • model model Property

    The model of the device. (read-only)

    Declaration

    Swift

    var model: String { get }

    Objective-C

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

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    var localizedModel: String { get }

    Objective-C

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

    Discussion

    The value of this property is a string that contains a localized version of the string returned from model.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The style of interface to use on the current device. (read-only)

    Declaration

    Swift

    var userInterfaceIdiom: UIUserInterfaceIdiom { get }

    Objective-C

    @property(nonatomic, readonly) UIUserInterfaceIdiom userInterfaceIdiom

    Discussion

    For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.2 and later.

  • An alphanumeric string that uniquely identifies a device to the app’s vendor. (read-only)

    Declaration

    Swift

    var identifierForVendor: NSUUID! { get }

    Objective-C

    @property(nonatomic, readonly, retain) NSUUID *identifierForVendor

    Discussion

    The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.

    Normally, the vendor is determined by data provided by the App Store. If the app was not installed from the app store (such as enterprise apps and apps still in development), then a vendor identifier is calculated based on the app’s bundle ID. The bundle ID is assumed to be in reverse-DNS format.

    • On iOS 6, the first two components of the bundle ID are used to generate the vendor ID. if the bundle ID only has a single component, then the entire bundle ID is used.

    • On IOS 7, all components of the bundle except for the last component are used to generate the vendor ID. If the bundle ID only has a single component, then the entire bundle ID is used.

    Table 1 shows a collection of bundle IDs and which portions of the bundle ID the system uses to calculate the vendor ID.

    Table 1Example bundle identifiers

    Bundle ID

    iOS 6.x

    iOS 7.x

    com.example.app1

    com.example.app1

    com.example.app1

    com.example.app2

    com.example.app2

    com.example.app2

    com.example.app.app1

    com.example.app.app1

    com.example.app.app1

    com.example.app.app2

    com.example.app.app2

    com.example.app.app2

    example

    example

    example

    For example, com.example.app1 and com.example.app2 would appear to have the same vendor ID.

    If the value is nil, wait and get the value again later. This happens, for example, after the device has been restarted but before the user has unlocked the device.

    The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them. The value can also change when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later.

  • The battery charge level for the device. (read-only)

    Declaration

    Swift

    var batteryLevel: Float { get }

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

  • A Boolean value indicating whether battery monitoring is enabled (YEStrue) or not (NOfalse).

    Declaration

    Swift

    var batteryMonitoringEnabled: Bool

    Objective-C

    @property(nonatomic, getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled

    Discussion

    Enable battery monitoring if your app needs to be notified of changes to the battery state, or if you want to check the battery charge level.

    The default value of this property is NOfalse, which:

    • Disables the posting of battery-related notifications

    • Disables the ability to read battery charge level and battery state

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

  • The battery state for the device. (read-only)

    Declaration

    Swift

    var batteryState: UIDeviceBatteryState { get }

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

  • A Boolean value indicating whether proximity monitoring is enabled (YEStrue) or not (NOfalse).

    Declaration

    Swift

    var proximityMonitoringEnabled: Bool

    Objective-C

    @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 NOfalse.

    Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityMonitoringEnabled property remains NOfalse, proximity monitoring is not available.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

    See Also

    proximityState

  • A Boolean value indicating whether the proximity sensor is close to the user (YEStrue) or not (NOfalse). (read-only)

    Declaration

    Swift

    var proximityState: Bool { get }

    Objective-C

    @property(nonatomic, readonly) BOOL proximityState

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

  • Plays an input click in an enabled input view.

    Declaration

    Swift

    func playInputClick()

    Objective-C

    - (void)playInputClick

    Discussion

    Use this method to play the standard system keyboard click in response to a user tapping in a custom input or keyboard accessory view. A click plays only if the user has enabled keyboard clicks in Settings > Sounds, and only if the input view is itself enabled and visible.

    To enable a custom input or accessory view for input clicks, perform the following two steps:

    1. Adopt the UIInputViewAudioFeedback protocol in your input view class.

    2. Implement the enableInputClicksWhenVisible delegate method to return YEStrue.

    For more information, see Text Programming Guide for iOS.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 4.2 and later.

  • The battery power state of the device.

    Declaration

    Swift

    enum UIDeviceBatteryState : Int { case Unknown case Unplugged case Charging case Full }

    Objective-C

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

    Constants

    • Unknown

      UIDeviceBatteryStateUnknown

      The battery state for the device cannot be determined.

      Available in iOS 3.0 and later.

    • Unplugged

      UIDeviceBatteryStateUnplugged

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

      Available in iOS 3.0 and later.

    • Charging

      UIDeviceBatteryStateCharging

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

      Available in iOS 3.0 and later.

    • Full

      UIDeviceBatteryStateFull

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

      Available in iOS 3.0 and later.

    Discussion

    These constants are used by the batteryState property.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.0 and later.

  • The physical orientation of the device.

    Declaration

    Swift

    enum UIDeviceOrientation : Int { case Unknown case Portrait case PortraitUpsideDown case LandscapeLeft case LandscapeRight case FaceUp case FaceDown } extension UIDeviceOrientation { var isLandscape: Bool { get } var isPortrait: Bool { get } var isFlat: Bool { get } var isValidInterfaceOrientation: Bool { get } } extension UIDeviceOrientation { var isLandscape: Bool { get } var isPortrait: Bool { get } var isFlat: Bool { get } var isValidInterfaceOrientation: Bool { get } }

    Objective-C

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

    Constants

    • Unknown

      UIDeviceOrientationUnknown

      The orientation of the device cannot be determined.

      Available in iOS 2.0 and later.

    • Portrait

      UIDeviceOrientationPortrait

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

      Available in iOS 2.0 and later.

    • PortraitUpsideDown

      UIDeviceOrientationPortraitUpsideDown

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

      Available in iOS 2.0 and later.

    • LandscapeLeft

      UIDeviceOrientationLandscapeLeft

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

      Available in iOS 2.0 and later.

    • LandscapeRight

      UIDeviceOrientationLandscapeRight

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

      Available in iOS 2.0 and later.

    • FaceUp

      UIDeviceOrientationFaceUp

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

      Available in iOS 2.0 and later.

    • FaceDown

      UIDeviceOrientationFaceDown

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

      Available in iOS 2.0 and later.

    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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The type of interface that should be used on the current device

    Declaration

    Swift

    enum UIUserInterfaceIdiom : Int { case Unspecified case Phone case Pad }

    Objective-C

    typedef enum { UIUserInterfaceIdiomUnspecified = -1 UIUserInterfaceIdiomPhone, UIUserInterfaceIdiomPad, } UIUserInterfaceIdiom;

    Constants

    • Unspecified

      UIUserInterfaceIdiomUnspecified

      Used when an object has a trait collection, but it is not in an environment yet. For example, a view that is created, but not put into a view hierarchy.

      Available in iOS 8.0 and later.

    • Phone

      UIUserInterfaceIdiomPhone

      The user interface should be designed for iPhone and iPod touch.

      Available in iOS 3.2 and later.

    • Pad

      UIUserInterfaceIdiomPad

      The user interface should be designed for iPad.

      Available in iOS 3.2 and later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.2 and later.