代码之家  ›  专栏  ›  技术社区  ›  drootang

如何让UI\u USER\u INTERFACE\u IDIOM()与iPhone OS SDK<3.2配合使用

  •  32
  • drootang  · 技术社区  · 16 年前

    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]
    }
    

    问题是在3.2之前的SDK中没有定义UI\u USER\u INTERFACE\u IDIOM()和UIUserInterfaceIdiomPad。这似乎完全违背了这样一个功能的目的。它们只能在iPhoneOS3.2上编译和运行(iPhoneOS3.2只能在iPad上运行)。因此,如果您可以使用UI\u USER\u INTERFACE\u IDIOM(),结果将始终是指示iPad。

    如果为了测试绑定到iPhone的通用应用程序代码而将此代码和目标OS 3.1.3(最新的iPhone/iPod Touch OS)包括在内,则在为iPhone simulator 3.1.3进行编译时,会出现编译器错误,因为3.1.3或更早版本中未定义符号。

    如果这是苹果推荐的运行时设备检测方法,我做错了什么?有人成功地使用这种方法来检测设备吗?

    8 回复  |  直到 16 年前
        1
  •  24
  •   Sangraal    16 年前

    我这样做是为了让代码在3.1.3和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
    }
    

    我也在这里写了一篇关于它的博客: http://www.programbles.com/2010/04/03/compiling-conditional-code-in-universal-iphone-ipad-applications/

        2
  •  14
  •   Ben Gottlieb    16 年前

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

    这是有条件编译的,因此您仍然可以为3.0 sim卡构建。然后检查UIDevice类是否响应选择器。如果这两个都失败了,那就不是iPad了。

        3
  •  7
  •   Community Mohan Dere    9 年前

    如果这是苹果推荐的方法 运行时 设备检测,我做错什么了?有人成功使用过吗

    这是我们的解决方案 运行时 检测:

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

    if (isIPhone) { ... }
    

    这与使用#if/#ifdef的区别在于,这是运行时测试,而#if是编译时测试。

    如果您的问题是编译时错误,那么您只需根据最新版本的SDK进行编译(另请参阅 How to access weak linked framework in iOS? ).

        4
  •  5
  •   drootang    16 年前

    模拟器

    无法针对3.2SDK进行编译并使用3.1.3或更早版本的模拟器。

        5
  •  5
  •   MattyG    14 年前

    - (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
    }
    
        6
  •  2
  •   sinh99    13 年前

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

    然后使用下面的检查

    #define DetailLabel_PosX (IS_IPAD ? 200 : 160)
    

    也有一些关于检查iphone5的定义

    #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 )
    
        7
  •  0
  •   user102008    15 年前

    这似乎完全违背了这样一个功能的目的。他们 只能在iPhone上编译和运行 OS 3.2(iPhone OS 3.2只能运行 在iPad上)。所以如果你能用 UI\u USER\u INTERFACE\u IDIOM(),结果 永远是指iPad。

    这是完全错误的。可能是的 运行

        8
  •  0
  •   occulus    15 年前

    UI\u USER\u INTERFACE\u IDIOM()和UIUserInterfaceIdiomPad可以在iOS3.2及更高版本上使用,重要的部分是“向上”。当然。iOS3.2只适用于iPad,但iOS4.0及更高版本在iphone和iPad上都可以运行,所以检查并不像你想象的那样毫无意义。

    推荐文章