如何检查设备是iPad还是iPhone无法正常工作

Ale*_*lin 4 uikit ios ios-simulator swift

我目前正在更新应用程序,我需要知道该应用程序是否在iPad中使用.

我在网上查了一下,发现下面的代码.我在Xcode中使用了iPad模拟器并运行了两个if语句.但每当我运行代码时,没有任何反应(打印消息不打印)这个代码是否适用于模拟器或我做错了什么?

当我检查它是否是UIUserInterfaceIdiom.phone时,print语句会执行,但我在模拟器中使用了iPad.

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad){
        print("This is an iPad")
        redoButton.layer.position.y -= 500



    }

if UIDevice.current.userInterfaceIdiom == .pad{
        print("iPad True")
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 11

if UIDevice.current.userInterfaceIdiom == .pad {
    print("iPad")
}else{
    print("not iPad")
}
Run Code Online (Sandbox Code Playgroud)

但是你需要让你的应用程序成为通用应用程序.


Awa*_*yaz 5

如果你想经常查看iphone/ipad。使用以下类

class Env {
  static var isIpad : Bool { return UIDevice.current.userInterfaceIdiom == .pad }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用

if Env.isIpad { // Ipad } 
else { iphone } 
Run Code Online (Sandbox Code Playgroud)