判断型号
Swift
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)))
}
OC
第一种方法
// 导入头文件
#include <sys/sysctl.h>
size_t size;
int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
第二种方法
// 导入头文件
#include <sys/utsname.h>
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
GODeviceModels
部分代码如下,最新代码移步 github
import UIKit
enum GODeviceModelsType {
case Unknown
case Simulator
case IPhone
...
}
class GODeviceModels {
static let shared = GODeviceModels()
var model = (GODeviceModelsType.Unknown, "Unknown")
private init() {
model = model(id: identifier())
}
}
extension GODeviceModels {
private func model(id: String) -> (type: GODeviceModelsType, string: String) {
switch id {
// MARK: - iPad
case "iPad1,1":
return (.IPad, "iPad")
// MARK: - iPad Air
case "iPad4,1", "iPad4,2", "iPad4,3":
return (.IPadAir, "iPad Air")
// MARK: - iPad Pro
case"iPad6,7", "iPad6,8":
return (.IPadPro_inch12_9, "iPad Pro (12.9-inch)")
// MARK: - iPad mini
case"iPad2,5", "iPad2,6", "iPad2,7":
return (.IPadMini, "iPad mini")
// MARK: - iPhone
case"iPhone1,1":
return (.IPhone, "iPhone")
// MARK: - iPod touch
case"iPod1,1":
return (.IPodTouch, "iPod touch")
// MARK: - Simulator
case "i386", "x86_64":
return (.Simulator, "Simulator")
default:
return (.Unknown, "Unknown")
}
}
}
extension GODeviceModels {
private func identifier() -> 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)))
}
return identifier
}
}
设备型号尺寸:
https://developer.apple.com/library/content/qa/qa1686/_index.html