Checking whether UI_USER_INTERFACE_IDIOM exists at

2019-08-15 09:46发布

问题:

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.

回答1:

You need to build apps for iPad against the 3.2 SDK. As such it will build correctly, and the UI_USER_INTERFACE_IDIOM() macro will still work. If you want to know how/why, look it up in the docs - it is a #define, which will be understood by the compiler and compile into code that will run correctly on 3.1 (etc).