我的应用程序需要知道哪个iphone设备正在运行,并根据它进行一些屏幕调整。 我使用以下代码来决定哪个iphone https://github.com/dennisweissmann/Basics/blob/master/Device.swift
但有没有办法找出是6S还是6S Plus? 所以关键是如果不是Device.swift中提到的设备之一,那么不知道如何调整屏幕并显示不支持的设备,但我希望该应用程序的行为与iPhone6S的iPhone6相同,并且相同作为适用于iPhone6s Plus的iPhone6 Plus。
谢谢
答案 0 :(得分:4)
您可以通过获取设备模型以编程方式执行此操作:
#import <sys/utsname.h>
NSString* deviceName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
结果模型:
@"i386" on 32-bit Simulator
@"x86_64" on 64-bit Simulator
@"iPod1,1" on iPod Touch
@"iPod2,1" on iPod Touch Second Generation
@"iPod3,1" on iPod Touch Third Generation
@"iPod4,1" on iPod Touch Fourth Generation
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPad1,1" on iPad
@"iPad2,1" on iPad 2
@"iPad3,1" on 3rd Generation iPad
@"iPhone3,1" on iPhone 4 (GSM)
@"iPhone3,3" on iPhone 4 (CDMA/Verizon/Sprint)
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada)
@"iPhone5,2" on iPhone 5 (model A1429, everything else)
@"iPad3,4" on 4th Generation iPad
@"iPad2,5" on iPad Mini
@"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
@"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
@"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPad4,1" on 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" on 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" on 2nd Generation iPad Mini - Wifi
@"iPad4,5" on 2nd Generation iPad Mini - Cellular
@"iPhone7,1" on iPhone 6 Plus
@"iPhone7,2" on iPhone 6
@"iPhone8,1" on iPhone 6S
@"iPhone8,2" on iPhone 6S Plus
答案 1 :(得分:3)
基于您的文件Device.swift, 插入:
case iPhone6S
case iPhone6SPlus
下:
case iPhone6Plus
并插入:
case "iPhone8,1": self = iPhone6S
case "iPhone8,2": self = iPhone6SPlus
下:
case "iPhone7,1": self = iPhone6Plus
答案 2 :(得分:1)
如果您使用AutoLayout
,则无需检查您的应用正在运行哪款iPhone,系统会自动进行更改。
答案 3 :(得分:0)
您可以使用此switch
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, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
switch identifier {
case "iPhone3,1", "iPhone3,2", "iPhone3,3", "iPhone4,1":
return "iPhone 4"
case "iPhone5,1", "iPhone5,2", "iPhone5,3", "iPhone5,4", "iPhone6,1", "iPhone6,2", "iPhone8,4":
return "iPhone 5"
case "iPhone7,2", "iPhone8,1", "iPhone9,1", "iPhone9,3":
return "iPhone 6,7"
case "iPhone7,1", "iPhone8,2", "iPhone9,2", "iPhone9,4":
return "iPhone Plus"
case "i386", "x86_64":
return "Simulator"
default:
return identifier
}
}
}
然后是我的代码:
switch UIDevice.current.modelName {
case "iPhone 4":
//Your code here
case "iPhone 5":
//Your code here
case "iPhone 6,7":
//Your code here
case "iPhone Plus", "Simulator":
//Your code here
default:
break
}