You’re first inclination might be to use the UIDevice class to check which device and OS version is installed. This means you have to remember when certain APIs were added and which device have which feature. Instead, you can check the presence and capabilities of a framework by asking for the framework class outright in your code. Apple has implemented class methods in each API to tell if the device has the specific functionality you are looking for. Here are a few example using the mail and camera capabilities:
Mail Composition
There might be a time when you need to send out a message through email. In versions prior to iPhone OS 3.0, you had to specifically open the mail application to access send an email. Now in iPhone OS 3, if a user has set up their account, you, as a developer, can imbed a modal view to send emails.
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
// We must always check whether the current device is configured for sending emails
if (mailClass != nil && [mailClass canSendMail])
{
[self displayComposerSheet];
}else{
[self launchMailAppOnDevice];
}
Camera Capabilities
There isn’t a camera in the iPod Touch, but that doesn’t mean there won’t be one in the future. If your application checked whether the device was an iPhone or iPod Touch, and Apple released a iPod Touch with a camera, your app would be inaccurate. With this code below, you don’t have to worry about new devices in the future.
Class photoClass = NSClassFromString(@"UIImagePickerController");
// Check is the device has a camera...
if(photoClass != nil && [photoClass isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"Camera Ready");
}else{
NSLog(@"No camera");
}
Lilly Medin
You are right on the money with this post, keep up the good work!