这是检测iPad的正确方法吗?

Mos*_*she 10 objective-c ipad ios

我可以使用以下代码来检测我的应用程序是否在iPad上运行吗?我的应用程序需要在iOS 3.0或更高版本上运行.

if([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]){
  //Do iPad stuff.
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*kin 28

UI_USER_INTERFACE_IDIOM()在iOS> = 3.2上使用宏:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   //device is an iPad.
}
Run Code Online (Sandbox Code Playgroud)

在早期版本的iOS上,您可以回退到您的代码,即:

NSRange ipadRange = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
if(ipadRange.location != NSNotFound) {
  //Do iPad stuff.
}
Run Code Online (Sandbox Code Playgroud)

这种方法是向前兼容的,因为如果明年苹果发布不同的iPad,模型名称可能会改变,但"iPad"这个词肯定会在字符串内部.


Jon*_*pan 5

不.改为:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)