dro*_*ang 32 iphone ipad iphone-sdk-3.2
Apple建议使用以下代码检测是在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]
}
Run Code Online (Sandbox Code Playgroud)
问题是在3.2之前的SDK中没有定义UI_USER_INTERFACE_IDIOM()和UIUserInterfaceIdiomPad.这似乎完全打败了这种功能的目的.它们只能在iPhone OS 3.2上编译和运行(iPhone OS 3.2只能在iPad上运行).因此,如果您可以使用UI_USER_INTERFACE_IDIOM(),结果将始终是指示iPad.
如果您包含此代码和目标OS 3.1.3(最新的iPhone/iPod Touch OS)以测试您的iPhone绑定的通用应用程序代码,您将收到编译器错误,因为符号未在3.1.3或更早版本中定义,为iPhone模拟器3.1.3编译时.
如果这是Apple推荐的运行时设备检测方法,那么我做错了什么?有没有人成功使用这种方法进行设备检测?
San*_*aal 24
我这样做是为了在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
}
Run Code Online (Sandbox Code Playgroud)
我还在这里写了一篇关于它的快速博客文章:http: //www.programbles.com/2010/04/03/compiling-conditional-code-in-universal-iphone-ipad-applications/
Ben*_*ieb 14
这是我使用的:
- (BOOL) amIAnIPad {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
if ([[UIDevice currentDevice] respondsToSelector: @selector(userInterfaceIdiom)])
return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
#endif
return NO;
}
Run Code Online (Sandbox Code Playgroud)
这有条件地编译,所以你仍然可以构建3.0 sim.然后检查UIDevice类是否响应选择器.如果其中任何一个失败,那就不是iPad了.
如果这是Apple推荐的运行时 设备检测方法,那么我做错了什么?有没有人成功使用这种方法进行设备检测?
这是运行时检测的解决方案:
#define isIPhone (![[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] || [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
Run Code Online (Sandbox Code Playgroud)
之后,您可以在代码中的任何位置轻松测试它:
if (isIPhone) { ... }
Run Code Online (Sandbox Code Playgroud)
这与使用#if/#ifdef的区别在于它是运行时测试,而#if是编译时测试.
我认为运行时测试更好,因为在这种情况下你可以使用一个完全适用于任何操作系统版本.如果使用编译时检查,则需要为不同的OS版本生成不同的可执行文件.
如果您的问题是编译时错误,那么您应该针对最新版本的SDK进行编译(另请参阅如何在iOS中访问弱链接框架?).
我相信答案就是不要试图在iPhone 模拟器 3.1.3或更早版本上运行代码.始终使用3.2 SDK进行编译.iPhone模拟器3.2将为您提供iPad模拟器,或为iPhone Device 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39816 次 |
| 最近记录: |