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


当前回答

在Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad

用法就像这样:

if UIDevice.current.userInterfaceIdiom == .pad {
    // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
    // Implement your logic here
}

其他回答

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

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad

我发现在Xcode中的模拟器中,有些解决方案并不适合我。相反,这是可行的:

ObjC

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

if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
    DebugLog(@"iPad");
} else {
    DebugLog(@"iPhone or iPod Touch");
}

斯威夫特

if UIDevice.current.model.hasPrefix("iPad") {
    print("iPad")
} else {
    print("iPhone or iPod Touch")
}

同样在Xcode中的“其他示例”中,设备模型返回为“iPad模拟器”,因此上述调整应该将其整理出来。

在Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad

用法就像这样:

if UIDevice.current.userInterfaceIdiom == .pad {
    // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
    // Implement your logic here
}

*

在swift 3.0中

*

 if UIDevice.current.userInterfaceIdiom == .pad {
        //pad
    } else if UIDevice.current.userInterfaceIdiom == .phone {
        //phone
    } else if UIDevice.current.userInterfaceIdiom == .tv {
        //tv
    } else if UIDevice.current.userInterfaceIdiom == .carPlay {
        //CarDisplay
    } else {
        //unspecified
    }

很多答案都很好,但我在swift 4中使用这样的答案

创建常量 struct App { 静态let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ?True: false } 像这样使用 if App.isRunningOnIpad { 返回加载(from: .main, identifier: identifier) }其他{ 返回加载(from: .ipad, identifier: identifier) }

编辑:建议ceure简单地在UIDevice上创建一个扩展

extension UIDevice {
    static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}