什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?
我得到一个使用未解决的标识符错误时,在Swift编译。
什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?
我得到一个使用未解决的标识符错误时,在Swift编译。
当前回答
供参考,我已经使用UI_USER_INTERFACE_IDIOM()为我的应用程序写在Swift。应用程序可以很好地使用XCode 6.3.1编译,没有任何警告,在模拟器(任何选择的设备)和我所有的真实设备(iPhone, iPad)上运行良好,iOS版本从7.1到8.3。
然而,这款应用在苹果评测者的设备上崩溃了(并被拒绝)。我花了几天时间才发现问题,并重新上传了几次到iTunes Connect。
现在我使用UIDevice.currentDevice()。取而代之的是userInterfaceIdiom,我的应用程序可以从这样的崩溃中幸存下来。
其他回答
Swift 2.0 & iOS 9 & Xcode 7.1
// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
// 2. check the idiom
switch (deviceIdiom) {
case .Pad:
print("iPad style UI")
case .Phone:
print("iPhone and iPod touch style UI")
case .TV:
print("tvOS style UI")
default:
print("Unspecified UI idiom")
}
Swift 3.0和Swift 4.0
// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
// 2. check the idiom
switch (deviceIdiom) {
case .pad:
print("iPad style UI")
case .phone:
print("iPhone and iPod touch style UI")
case .tv:
print("tvOS style UI")
default:
print("Unspecified UI idiom")
}
使用UITraitCollection。 iOS trait环境是通过UITraitEnvironment协议的traitCollection属性公开的。以下类采用此协议:
UIScreen ui窗口 ui UIPresentationController UIView
当使用Swift时,你可以使用enum UIUserInterfaceIdiom,定义为:
enum UIUserInterfaceIdiom : Int {
case unspecified
case phone // iPhone and iPod touch style UI
case pad // iPad style UI (also includes macOS Catalyst)
}
所以你可以这样使用它:
UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified
或者使用Switch语句:
switch UIDevice.current.userInterfaceIdiom {
case .phone:
// It's an iPhone
case .pad:
// It's an iPad (or macOS Catalyst)
@unknown default:
// Uh, oh! What could it be?
}
UI_USER_INTERFACE_IDIOM()是一个Objective-C宏,它被定义为:
#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)
还要注意,即使在使用Objective-C时,UI_USER_INTERFACE_IDIOM()宏也只在针对iOS 3.2及以下时才需要。当部署到iOS 3.2及以上版本时,可以直接使用[UIDevice userInterfaceIdiom]。
尝试添加这样的扩展:
public extension UIDevice {
var modelName: 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)))
}
switch identifier {
case "iPod5,1": return "iPod Touch 5"
case "iPod7,1": return "iPod Touch 6"
case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
case "iPhone4,1": return "iPhone 4s"
case "iPhone5,1", "iPhone5,2": return "iPhone 5"
case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
case "iPhone7,2": return "iPhone 6"
case "iPhone7,1": return "iPhone 6 Plus"
case "iPhone8,1": return "iPhone 6s"
case "iPhone8,2": return "iPhone 6s Plus"
case "iPhone9,1", "iPhone9,3": return "iPhone 7"
case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
case "iPhone8,4": return "iPhone SE"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
case "iPad5,3", "iPad5,4": return "iPad Air 2"
case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
case "iPad5,1", "iPad5,2": return "iPad Mini 4"
case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
case "AppleTV5,3": return "Apple TV"
case "i386", "x86_64": return "Simulator"
default: return identifier
}
}
}
下面是你如何使用它:
let modelName = UIDevice.currentDevice().modelName
编辑 对于模拟器,您可以在这里尝试解决方案
在swift 4和Xcode 9.2中,你可以通过以下方法来检测设备是否是iPhone/iPad。
if (UIDevice.current.userInterfaceIdiom == .pad){
print("iPad")
}
else{
print("iPhone")
}
另一种方式
let deviceName = UIDevice.current.model
print(deviceName);
if deviceName == "iPhone"{
print("iPhone")
}
else{
print("iPad")
}
你可以在Swift 5上使用新的方式:
switch traitCollection.userInterfaceIdiom {
case .unspecified:
// do something
case .phone:
// do something
case .pad:
// do something
case .tv:
// do something
case .carPlay:
// do something
case .mac:
// do something
@unknown default:
// do something
}