Friday, February 5, 2016

Objective C fetching device details

We can get device details on which our app is running using the UIDevice class in UIKit framework.

In this post let's see some of the needy details that we can get from this UIDevice class.


Though we have auto layouts, auto resizing to build user interface screens for iOS apps, some times we need to know whether our app is running on an iPad or on an iPhone. We can get the using UIUserInterfaceIdiom which is having two values.

  •     UIUserInterfaceIdiomPhone,          
  •     UIUserInterfaceIdiomPad

  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){

       // App is running on iPad


       // App is running on iPhone



We may need it occasionally. This is the device name that user has set in the device. If we need it to display the name of the device we can use this property to fetch as shown below.

    [UIDevice currentDevice] name]


It's is mandatory now a days to check the iOS version and program as per the new features along with taking deprecated things into consideration. We can fetch the device system version using the systemVersion property as shown below and write our customizations.

  if([[[UIDevice currentDevice] systemVersion] isEqualToString:@"7.0"]){



Device orientation checking is very important if we are supporting all the orientations. We can fetch current orientation of the device using this orientation property which  gives one of the below listed values based on the device current orientation.

   [UIDevice currentDevice] orientation]

  •     UIDeviceOrientationUnknown,
  •     UIDeviceOrientationPortrait,  
  •     UIDeviceOrientationPortraitUpsideDown,  
  •     UIDeviceOrientationLandscapeLeft,       
  •     UIDeviceOrientationLandscapeRight,      
  •     UIDeviceOrientationFaceUp,              
  •     UIDeviceOrientationFaceDown            


If our app is having background services like location or weather changes, it is a good practice to check the device battery level and handle the things. If the battery level is very low, better to stop these services for a while and alert the user about the battery state. We can fetch the battery level of the device using this batteryLevel property.

This value ranges from 0 - 1.0 and gives -1.0 if the battery state is unknown.


If we have a scenario like, we need to stop some activities in the app if the device is getting charged. This detail we can fetch using batteryState property. It gives one of below listed values.

  •     UIDeviceBatteryStateUnknown,
  •     UIDeviceBatteryStateUnplugged,     // on battery, discharging
  •     UIDeviceBatteryStateCharging,       // plugged in, less than 100%
  •     UIDeviceBatteryStateFull,        

Hope this post is useful. Feel free to comment in case of any queries.

No comments:

Post a Comment