我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?


当前回答

有很多方法可以检查设备是否为iPad。这是我最喜欢的检查设备是否真的是iPad的方法:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}

我使用它的方式

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad

if ( IDIOM == IPAD ) {
    /* do something specifically for iPad. */
} else {
    /* do something specifically for iPhone or iPod touch. */
}   

其他的例子

if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
    return YES; /* Device is iPad */
}

#define IPAD     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD ) 
     return YES;

有关Swift解决方案,请参阅以下答案:https://stackoverflow.com/a/27517536/2057171

其他回答

UI_USER_INTERFACE_IDIOM()只返回iPad如果应用程序是iPad或通用。如果这是一款运行在iPad上的iPhone应用,那么它就不会这么做。所以你应该检查模型。

为什么这么复杂?我就是这么做的…

斯威夫特4:

var iPad : Bool {
    return UIDevice.current.model.contains("iPad")
}

这样你就可以说if iPad {}

你也可以用这个

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
   // iPad
} else {
   // iPhone / iPod Touch
}

你可以检查rangeOfString来查看单词iPad是否像这样存在。

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;

if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound)  {
NSLog(@"I am an iPad");
} else {
NSLog(@"I am not an iPad");
}

这是iOS 3.2的UIDevice的一部分,例如:

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad