userInterfaceIdiom在实际设备上返回错误的值

时间:2016-01-28 13:50:17

标签: ios swift ipad uidevice

我的应用被拒绝了,因为我的应用的iPad用户界面存在问题。我通过检查设备是否是iPad对UI测量进行一些微小的调整来解决这个问题。问题是,当我打电话时:

UIDevice.currentDevice().userInterfaceIdiom //returns .Unspecified
traitCollection.userInterfaceIdiom //returns -1

在真正的iPad上,我得到了。未指定。这是为什么?

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我创建了一个实际上可以在真正的iPad上运行的自定义方法

public extension UIDevice {

func isIpadDevice() -> Bool {
    let identifier = deviceIdentifier()

    return identifier.lowercaseString.containsString("ipad")
}

private func deviceIdentifier() -> String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8 where value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    return identifier
}

}

为了使用它,请致电

UIDevice.currentDevice().isIpadDevice() //wohoo