Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking whether UI_USER_INTERFACE_IDIOM exists at runtime
    text
    copied!<p>I am working on a universal app that should be able to run on iPad and iPhone. The Apple iPad docs say to use <code>UI_USER_INTERFACE_IDIOM()</code> to check if I am running on iPad or iPhone, but our iPhone is 3.1.2 and will not have <code>UI_USER_INTERFACE_IDIOM()</code> defined. As such, this code breaks:</p> <pre><code>//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; } } </code></pre> <p>In Apple's <a href="http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-1114537-BABHHJBC" rel="nofollow noreferrer">SDK Compatibility Guide</a> they suggest doing the following to check if the function exists:</p> <pre><code>//iPhone should not be flipped upside down. iPad can have any - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(UI_USER_INTERFACE_IDIOM() != NULL &amp;&amp; UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return YES; //are we on an iPad? } else { return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown; } } </code></pre> <p>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 <code>(void *)</code>:</p> <pre><code>//iPhone should not be flipped upside down. iPad can have any - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if((void *)UI_USER_INTERFACE_IDIOM() != NULL &amp;&amp; UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return YES; //are we on an iPad? } else { return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown; } } </code></pre> <p>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.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload