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
UIImagePickerControllerSourceTypeCameravalue to this method.The Photo Library, by passing the
UIImagePickerControllerSourceTypePhotoLibraryvalue to this method. This browses the root folder of the Photos directory on the device.The camera ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access