This is the final tip in a series of four that is based on content from the O’Reilly book Head First iPhone Development. O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by referring a friend to this tip – see the Refer-a-Friend button near the bottom of this post.
This tip is from Chapter 9 – Camera, Mapkit and Core Location and shows how to make a quick check on the status/availability of the camera on device.
We are no longer accepting entries for today’s giveaway.
Is the Camera Available and Ready?
Not all devices support the same source media capabilities, for example, there is no currently no camera on the iPod touch. In addition, even if a device does support a specific media source, that source could be busy – for example, the camera on an iPhone may be in use.
This short snippet of code demonstrates how to check if a device has a camera and/or if the camera is available:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) NSLog(@"Camera is available and ready"); else NSLog(@"Camera is not available");
Note that when using the Simulator within Xcode, this call will return NO, as there is no camera available.
Does iPhone Support Recording Video?
The only iPhone to support video recording is the 3GS. There are two checks required before you can use the technology in your application. First, check to see if a camera is available, and second, verify the media types available on the device includes movies.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) NSLog(@"Camera is available and ready"); else NSLog(@"Camera is not available"); NSArray *media = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera]; if ([media containsObject:@"public.movie"] == YES) NSLog(@"Video recording available"); else NSLog(@"Video recording is not available");