Cover by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

13.1. Detecting and Probing the Camera

Problem

You want to know whether the iOS device running your application has a camera that you can access. This is an important check to make before attempting to use the camera, unless you are sure your application will never run on a device that lacks one.

Solution

Use the isSourceTypeAvailable: class method of UIImagePickerController with the UIImagePickerControllerSourceTypeCamera value, like so:

- (BOOL) isCameraAvailable{
  
  return [UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera];
  
}

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  
  if ([self isCameraAvailable]){
    NSLog(@"Camera is available.");
  } else {
    NSLog(@"Camera is not available.");
  }
  
  self.window = [[UIWindow alloc] initWithFrame:
                 [[UIScreen mainScreen] bounds]];
  
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  return YES;
}

Discussion

Before attempting to display an instance of UIImagePickerController to your user for taking photos or shooting videos, you must detect whether the device supports that interface. The isSourceTypeAvailable: class method allows you to determine three sources of data:

  • The camera, by passing the UIImagePickerControllerSourceTypeCamera value to this method.

  • The Photo Library, by passing the value UIImagePickerControllerSourceTypePhotoLibrary to this method. This browses the root folder of the Photos directory on the device.

  • The camera ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required