How does one get UI_USER_INTERFACE_IDIOM() to work with iPhone OS SDK < 3.2
Asked Answered
P

8

32

Apple advises using the following code to detect whether running on an iPad or iPhone/iPod Touch:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  // The device is an iPad running iPhone 3.2 or later.
  // [for example, load appropriate iPad nib file]
}
else {
  // The device is an iPhone or iPod touch.
  // [for example, load appropriate iPhone nib file]
}

The problem is that UI_USER_INTERFACE_IDIOM() and UIUserInterfaceIdiomPad are NOT defined in the SDKs prior to 3.2. This seems to completely defeat the purpose of such a function. They can only be compiled and run on iPhone OS 3.2 (iPhone OS 3.2 can only be run on iPad). So if you can use UI_USER_INTERFACE_IDIOM(), the result will always be to indicate an iPad.

If you include this code and target OS 3.1.3 (the most recent iPhone/iPod Touch OS) in order to test your iPhone-bound universal app code, you will get compiler errors since the symbols are not defined in 3.1.3 or earlier, when compiling for iPhone simulator 3.1.3.

If this is the recommended-by-Apple approach to runtime device-detection, what am I doing wrong? Has anyone succeeded using this approach to device-detection?

Petronia answered 5/4, 2010 at 0:7 Comment(0)
B
24

I do this to get the code to compile in both 3.1.3 and 3.2:

BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
// iPad specific code here
} else {
// iPhone/iPod specific code here
}

I also wrote a quick blog post about it here: http://www.programbles.com/2010/04/03/compiling-conditional-code-in-universal-iphone-ipad-applications/

Buitenzorg answered 9/4, 2010 at 15:41 Comment(0)
L
14

This is what I use:

- (BOOL) amIAnIPad {
    #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
        if ([[UIDevice currentDevice] respondsToSelector: @selector(userInterfaceIdiom)])
            return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
    #endif
    return NO;
}

This conditionally compiles, so you can still build for the 3.0 sim. It then checks to see if the UIDevice class responds to the selector. If either of these fail, it's not an iPad.

Lelea answered 5/4, 2010 at 3:51 Comment(2)
That is if you want to compile for 3.0. You should, as drootang said, compile for 3.2 and run it on a 3.0 device, because 3.2 is what your submitting to the AppStore.Nonproductive
I agree, but there's no way to test in the iPHONE Simulator (as opposed to iPad) while building for 3.2. If you want to test on both sims, you have to do this kind of trickery.Lelea
M
7

If this is the recommended-by-Apple approach to runtime device-detection, what am I doing wrong? Has anyone succeeded using this approach to device-detection?

This is solution for runtime detection:

#define isIPhone (![[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] || [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

After that you can easily test it anywhere in your code:

if (isIPhone) { ... }

The difference between this and using #if / #ifdef is that this is runtime testing, while #if is compile-time testing.

I think the runtime testing is better, because you can use one exactable for any OS version in this case. If you use compile-time check, you'll need to produce different executables for different OS versions.

If your problem is compile-time errors, you just should compile against last version of SDK (see also How to access weak linked framework in iOS?).

Mincey answered 5/1, 2012 at 19:32 Comment(0)
P
5

I believe the answer is simply do not attempt to run the code on iPhone simulator 3.1.3 or earlier. Always compile with a 3.2 SDK. The iPhone simulator 3.2 will get you the iPad simulator, or compile for iPhone Device 3.2 and put the app on a phone to test it.

There is no way to compile against 3.2 SDK and use a 3.1.3 or earlier simulator.

Petronia answered 5/4, 2010 at 4:13 Comment(1)
yes there is. You can build for 3.2 SDK then change the current target to iPhone 3.1 then do either "Run" or "Debug" don't rebuild it.Danaus
H
5

Instead of any compiler based stuff I use:

- (BOOL)deviceIsAnIPad {
if ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)])
    //We can test if it's an iPad. Running iOS3.2+
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        return YES; //is an iPad
    else 
        return NO; //is an iPhone
else 
    return NO; //does not respond to selector, therefore must be < iOS3.2, therefore is an iPhone
}
Hopscotch answered 17/4, 2012 at 7:32 Comment(0)
S
2

Declare using this

#ifdef UI_USER_INTERFACE_IDIOM
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define IS_IPAD false
#endif

then use check as below

#define DetailLabel_PosX (IS_IPAD ? 200 : 160)

There are also some define for checking iphone 5

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define IOS_OLDER_THAN_6 ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0 )
Survival answered 26/7, 2012 at 7:20 Comment(0)
A
0

This seems to completely defeat the purpose of such a function. They can only be compiled and run on iPhone OS 3.2 (iPhone OS 3.2 can only be run on iPad). So if you can use UI_USER_INTERFACE_IDIOM(), the result will always be to indicate an iPad.

This is completely incorrect. It can be compiled on Base SDK of 3.2, but it can be run on any OS, if you set the deployment target appropriately.

Arrowood answered 12/3, 2011 at 0:31 Comment(0)
D
0

UI_USER_INTERFACE_IDIOM() and UIUserInterfaceIdiomPad can be used on iOS3.2 and upwards, the important part being the 'upwards'. Sure. iOS3.2 is only for iPad, but iOS4.0 and beyond run on both iPhones and iPads, so the check isn't as pointless as you think.

Davey answered 28/5, 2011 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.