怎么开始UI_USER_INTERFACE_IDIOM()与iPhone OS SDK 3.2工作<
-
24-09-2019 - |
题
苹果建议使用以下代码来检测是否在iPad或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]
}
的问题是,UI_USER_INTERFACE_IDIOM()和UIUserInterfaceIdiomPad在之前3.2的SDK没有定义。这似乎彻底击垮这样的功能的目的。它们只能被编译并在iPhone OS 3.2(iPhone OS 3.2也只能在iPad上运行)上运行。所以,如果你可以使用UI_USER_INTERFACE_IDIOM(),结果总是会指示一台iPad。
如果您在以测试你的iPhone绑定通用的应用程序代码,这些代码和目标OS 3.1.3(最新的iPhone / iPod Touch的操作系统),你会因为符号是不是在3.1定义的获取编译器错误。 3或更早的时候,编译为iPhone模拟器3.1.3。
如果这是推荐的,通过苹果的方法来运行设备的检测,我究竟做错了什么?具有使用这种方法设备检测任何人成功?
解决方案
我这样做是为了获得代码在两个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/
其他提示
这是我使用:
- (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。
如果这是推荐的逐苹果方法为运行时强> 设备检测,我究竟做错了什么?有没有人使用成功 这种方法设备检测?
这是的运行时强>检测溶液:
#define isIPhone (![[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] || [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
之后,你可以随时随地轻松测试它在你的代码:
if (isIPhone) { ... }
此使用的#if之间的差/ #ifdef来的是,这是运行时检测,而是的#if编译时测试。
我想运行测试是更好的,因为你可以使用一个碱解在这种情况下,任何操作系统版本。如果您使用的编译时检查,就需要产生不同的可执行文件针对不同的操作系统版本。
如果你的问题是编译时错误,你就应该编译针对SDK的最新版本(见的如何在iOS的访问弱链接框架?)。
我相信答案是根本就没有尝试运行在iPhone上的代码的模拟的3.1.3或更早版本。总是以3.2 SDK编译。 iPhone模拟器3.2将让你在iPad模拟器,或编译iPhone设备3.2,并把应用程序在手机上进行测试。
有没有办法进行编译3.2 SDK并使用3.1.3或更早模拟器。
代替任何基于编译器的东西,我使用:
- (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
}
使用此
的Declare#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)
也有一些限定用于检查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 )
这似乎完全战胜这种功能的目的。他们 只能被编译并在iPhone上运行 OS 3.2(iPhone OS 3.2只能运行 在iPad上)。所以,如果你可以使用 UI_USER_INTERFACE_IDIOM(),其结果 总是会以指示一个iPad。
这是完全不正确。它可以是的编译的对3.2 SDK基地,但它可以的运行的任何操作系统上,如果你设置适当的部署目标。
UI_USER_INTERFACE_IDIOM()和UIUserInterfaceIdiomPad可以iOS3.2并且向上被使用,重要的部分是所述“向上”。当然。 iOS3.2仅适用于iPad,但IOS4.0及以后两个iPhone和iPad的运行,所以检查是不是毫无意义,你的想法。