【发布时间】:2010-12-31 02:39:55
【问题描述】:
我可以使用以下代码来检测我的应用程序是否在 iPad 上运行?我的应用需要在 iOS 3.0 或更高版本上运行。
if([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]){
//Do iPad stuff.
}
【问题讨论】:
标签: objective-c ipad ios
我可以使用以下代码来检测我的应用程序是否在 iPad 上运行?我的应用需要在 iOS 3.0 或更高版本上运行。
if([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]){
//Do iPad stuff.
}
【问题讨论】:
标签: objective-c ipad ios
在 iOS >= 3.2 上使用 UI_USER_INTERFACE_IDIOM() 宏:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//device is an iPad.
}
在早期版本的 iOS 上,您可以回退到您的代码,即:
NSRange ipadRange = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
if(ipadRange.location != NSNotFound) {
//Do iPad stuff.
}
这种方法是向前兼容的,如果明年 Apple 发布不同的 iPad,型号名称可能会改变,但“iPad”这个词肯定会在字符串中的某个位置。
【讨论】:
不。改为这样做:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// ...
}
【讨论】:
UI_USER_INTERFACE_IDIOM() 的较新 SDK。它是一个宏,在早期的操作系统版本中,它总是返回UIUserInterfaceIdiomPhone。如果您使用的是旧版 SDK,您将无法进行 iPad 特定开发,因为您将无法启用 iPad 定位。