Home:ALL Converter>Checking whether UI_USER_INTERFACE_IDIOM exists at runtime

Checking whether UI_USER_INTERFACE_IDIOM exists at runtime

Ask Time:2010-04-16T23:53:01         Author:MrHen

Json Formatter

I am working on a universal app that should be able to run on iPad and iPhone. The Apple iPad docs say to use UI_USER_INTERFACE_IDIOM() to check if I am running on iPad or iPhone, but our iPhone is 3.1.2 and will not have UI_USER_INTERFACE_IDIOM() defined. As such, this code breaks:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

In Apple's SDK Compatibility Guide they suggest doing the following to check if the function exists:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

This works, but results in the compiler warning: "Comparison between pointer and integer." After digging around I figured out that I can make the compiler warning disappear with the following cast to (void *):

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if((void *)UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

My question is this: Is the last code block here okay/acceptable/standard practice? I couldn't find anyone else doing something like this with quick searching which makes me wonder if I missed a gotcha or something similar.

Thanks.

Author:MrHen,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2654360/checking-whether-ui-user-interface-idiom-exists-at-runtime
yy