Best way to programmatically detect iPad/iPhone hardware

前端 未结 10 869
借酒劲吻你
借酒劲吻你 2020-11-27 14:14

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad

相关标签:
10条回答
  • 2020-11-27 14:50

    My solution (works on 3.2+):

    #define IS_IPHONE (!IS_IPAD)
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
    

    then,

    if (IS_IPAD)
        // do something
    

    or

    if (IS_IPHONE)
        // do something else
    
    0 讨论(0)
  • 2020-11-27 14:53

    Checking at runtime (your first way) is completely different from #if at compile time. The preprocessor directives won't give you a universal app.

    The preferred way is to use Apple's Macro:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
         // The device is an iPad running iPhone 3.2 or later.
    }
    else
    {
         // The device is an iPhone or iPod touch.
    }
    

    Use 3.2 as the base SDK (because the macro is not defined pre 3.2), you can target prior OS versions to get it running on the iPhone.

    0 讨论(0)
  • 2020-11-27 14:58

    If you are using features that are not backwards compatible, I found the best way for me is to create a #define in the pre-compiled header. Example:

    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
    #define USING_4_X
    #endif
    

    Then in your code, you can do this:

    BOOL exists = NO;
    #ifdef USING_4_X        
    exists = [SomeObject someMethod:[url lastPathComponent]];
    #else
    exists = [SomeObject someMethod:[[url path] lastPathComponent]];
    #endif
    
    0 讨论(0)
  • 2020-11-27 15:03

    If 1- you already have the app installed into your device, 2- you change its build settings to be a 'Universal' app, 3- install the app to your device on top of the pre-existing app (without deleting the previous one)

    You might find that the solutions provided here to detect iPhone/iPad do not work. First, delete the app that was 'only' for iPad/iPhone and install it fresh to your device.

    0 讨论(0)
提交回复
热议问题