iPhone Development: UIDevice

No Image

Since the release of the iPhone OS it has been used on a number of phones, iPod Touch and iPad models and will likely continue to be used as Apple come up with new devices - particularly those coming soon using the iPhone OS 4.0 currently in development. Sometimes you need to be able to code specific to a device which means you need a way to identify the UDID, the Operating System version, and the model.

Each device with iPhone OS installed can be identified by a unique identifier and other properties about the status of the device. To get details about the device we first need to instantiate a UIDevice object which we can then use to query properties with.

UIDevice myDevice = [UIDevice currentDevice];

From this we can then query:

  • model - The model of the device, for example iPhone, iPod touch, or iPad.
  • localizedModel - This produces the same as the model string but will use the "localised" name for it, by this it means it will apply any changes due to language.
  • systemName - the name of the operating system running on the device, e.g. iPhone OS.
  • systemVersion - This is the version of the operating system, so for up-to-date devices this will currently be 3.2.
  • name - This is the name of your device, i.e. it is what you've called it from the settings or via iTunes.
  • uniqueIdentifier - This is a unique string which is guaranteed to be different on every device.
  • userInterfaceIdiom - The style of interface to use on the current device - this is something which was added in version 3.2 and is used to return either UIUserInterfaceIdiomPhone or UIUserInterfaceIdiomPad.

If we just wanted to get one property an do nothing else with the UIDevice object the easiest way to do so would be as follows:

NSLog(@"UID: %@", [[UIDevice currentDevice] uniqueIdentifier]);

You can actually use this method to determine whether or not your application is running on the iPhone Simulator - if it is then this can be detected from model, localizedModel and name. So far we've looked at read-only properties that aren't going to change that often (most of them will only change when you upgrade the firmware), but there are other properties this class can return which will change during operation sometimes - such as the orientation of the device and properties used to determine if certain pieces of data can be retrieved.

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
    ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {

The above code will evaluate to true of the device is in landscape mode with either it's left edge or right edge at the bottom. Being able to detect orientation and device using these methods mean that we can change the layout or XIB based upon how the device is orientated or which device it is.

Also available are methods for checking the battery level, and the proximity sensor's current status. In the case of the battery level you can't read this property until batteryMonitoringEnabled is set. Unlike a lot of the UIDevice properties this one is not read-only so can be used to enable battery monitoring so you can retrieve the current battery level. With this enabled you can also monitor the battery state to determine if the device is currently unplugged from the charger, charging, or has a full battery.

your comments - Post a comment

blog comments powered by Disqus