What device am I running on?

That is often the wrong question to ask. Because hardware and software will change, future versions of, say, the iPad, may have functions they don't have now, for example a true GPS or a camera. It is much better to test for the particular functionality you want to use.

Here is a very incomplete list of things an app might want to know about the device and iOS version it is running on.

Does this version of iOS support a certain feature?

See Apple's SDK Compatibility Guide.

Does a class or object respond to a certain method?

This is usually the best way to inquire about a feature. The code will look something like this,

 if ([objectOrClass respondsToSelector: @selector(nameOfSelector)])
    [objectOrClass nameOfSelector];  //It's safe to call this method
 else
    {
      //Do something else or post an alert
    }; 

The answer is yes if the objectOrClass will handle that method and no if it cannot. The inquiry includes the superclasses of objectOrClass.

Am I running on an iPad?

bool runningOniPad()
{
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
  if ([[UIDevice currentDevice]
                 respondsToSelector: @selector(userInterfaceIdiom)])
   return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
  else
   return NO;
#else
  return NO;
#endif 
}

Other things to learn from UIDevice: name, model, systemVersion, batteryLevel, etc. See UIDevice Class Reference.

What is the window size?

CGRect c = [window bounds];

Does this device have a Retina display?

BOOL isRetinaDisplay()
{
  CGFloat scale = 1.0;
  UIScreen *screen = [UIScreen mainScreen];
  if([screen respondsToSelector:@selector(scale)])
      scale = screen.scale;
          
  if(scale == 2.0f)
   return YES;
  else return NO;
}

Does this device have a Camera?

BOOL hasCamera()
{
 return UIImagePickerController
    isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];      
}

Does it have a Video camera?

You must #import <MobileCoreServices/UTCoreTypes.h>

MobileCoreServices framework must be in the project.

BOOL isVideoCameraAvailable()
{
  UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  NSArray *sourceTypes = [UIImagePickerController
                       availableMediaTypesForSourceType:picker.sourceType];
  [picker release];
  if ([sourceTypes containsObject:(NSString *)kUTTypeMovie ])
   return YES;
  else
   return NO;
}

Does it have a Microphone?

AVFoundation framework must be in the project.
You must #import <AVFoundation/AVFoundation.h>

BOOL isMicrophoneAvailable()
{
  AVAudioSession *ptr = [AVAudioSession sharedInstance];
  return ptr.inputIsAvailable;
}

Does it have a magnetometer (compass)?

CoreLocation framework must be in the project.

You must #import <CoreLocation/CoreLocation.h>

- (void) setupMagnetometer
{
  // setup the location manager
  self.locationManager = [[[CLLocationManager alloc] init] autorelease];
  // check if the hardware has a compass
  if (locationManager.headingAvailable == NO) {
   // No compass is available. 
   // so a dialog will be displayed and no magnetic data will be measured.
   self.locationManager = nil;
   UIAlertView *noCompassAlert = [[UIAlertView alloc]
                  initWithTitle:@"No Compass!"
                  message:@"This device cannot measure magnetic fields."
                  delegate:nil
                  cancelButtonTitle:@"OK"
                  otherButtonTitles:nil];
   [noCompassAlert show];
   [noCompassAlert release];
   }
  else
   {
   // heading service configuration
   locationManager.headingFilter = kCLHeadingFilterNone;
   
   // setup delegate callbacks
   locationManager.delegate = self;
   
   // start the compass
   [locationManager startUpdatingHeading];
  }
 }

Does it have a gyroscope?

CoreMotion framework must be in the project.

You must #import <CoreMotion/CMMotionManager.h>

BOOL isGyroscopeAvailable()
{
#ifdef __IPHONE_4_0
 CMMotionManager *motionManager = [[CMMotionManager alloc] init];
 BOOL gyroAvailable = motionManager.gyroAvailable;
 [motionManager release];
 return gyroAvailable;
#else
 return NO;
#endif
 }

Does this version of iOS do Multitasking?

BOOL isMultitaskingCapable()
{
 UIDevice* device = [UIDevice currentDevice];
 if ([device respondsToSelector:@selector(isMultitaskingSupported)])
  return device.multitaskingSupported;
 else
  return NO;
}

All recent versions of iOS are multitasking, so this question is relevant only if you want to run on the earliest versions.

A blog with comments on this topic.

The page was last updated Saturday, May 12, 2012 9:31 AM