11.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
UIImagePickerControllerSourceTypePhoto
Library
value to this method. This browses the root folder of the Photos directory on the device.The camera ...
Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.