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

后端 未结 8 1513
旧时难觅i
旧时难觅i 2020-11-28 04:30

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

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {         


        
相关标签:
8条回答
  • 2020-11-28 05:09

    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 )
    
    0 讨论(0)
  • 2020-11-28 05:10

    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/

    0 讨论(0)
  • 2020-11-28 05:19

    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?).

    0 讨论(0)
  • 2020-11-28 05:20

    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.

    0 讨论(0)
  • 2020-11-28 05:26

    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.

    0 讨论(0)
  • 2020-11-28 05:27

    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
    }
    
    0 讨论(0)
提交回复
热议问题