iPhone “设置” -> “通用“ -> “关于本机” 中的软件编译版本号如下图所示:
那么如何通过代码去获取软件编译版本号呢?其实我们可以通过sysctlbyname()
函数去获取,该函数用于获取操作系统有关内核和硬件的详细信息。
1. 获取“关于本机”中前三项内容
1.1 名称
NSString *deviceName = [[UIDevice currentDevice] name];
NSLog(@"名称: %@", deviceName);
1.2 软件版本
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// kern.osproductversion 例如: 15.4
// NSString *systemVersion = [self readSystemInfoByName:"kern.osproductversion"];
// kern.osversion 例如: 19E241
NSString *systemBuildVersion = [self readSystemKernelInfoByID:KERN_OSVERSION];
NSLog(@"软件版本: %@ (%@)", systemVersion, systemBuildVersion);
1.3 型号名称
NSString *deviceModelName = [self deviceModelName];
// hw.product 例如: iPhone10,3
// NSString *deviceMode = [self readSystemHardwareInfoByID:HW_PRODUCT];
NSLog(@"型号名称: %@", deviceModelName);
2. 读取系统内核信息及系统硬件信息的方法
/// 读取系统内核信息
/// @param ID 信息标识, 范围: [KERN_OSTYPE, KERN_MAXID)
- (NSString *)readSystemKernelInfoByID:(UInt8)ID {
// 需要导入: #import <sys/sysctl.h>
NSString *info = nil;
// ID范围: [KERN_OSTYPE, KERN_MAXID)
if (ID >= KERN_MAXID) {
return info;
}
struct ctlname ctlNames[] = CTL_NAMES;
struct ctlname ctlName = ctlNames[CTL_KERN];
// 内核信息标识列表
struct ctlname names[] = CTL_KERN_NAMES;
struct ctlname name = names[ID];
// 拼接信息名称 (例如: kern.osrelease)
char *infoName = malloc(64);
strcat(infoName, ctlName.ctl_name);
strcat(infoName, ".");
strcat(infoName, name.ctl_name);
// NSLog(@"Kernel infoName: %s", infoName);
size_t size;
sysctlbyname(infoName, NULL, &size, NULL, 0);
if (name.ctl_type == CTLTYPE_STRING) {
char *var = malloc(size);
sysctlbyname(infoName, var, &size, NULL, 0);
if (var != NULL) {
info = [NSString stringWithCString:var encoding:NSUTF8StringEncoding];
}
free(var);
} else if (name.ctl_type == CTLTYPE_INT) {
int64_t var;
sysctlbyname(infoName, &var, &size, NULL, 0);
info = [NSString stringWithFormat:@"%lld", var];
}
free(infoName);
return info;
}
/// 读取系统硬件信息
/// @param ID 信息标识, 范围: [HW_MACHINE, HW_MAXID)
- (NSString *)readSystemHardwareInfoByID:(UInt8)ID {
// 需要导入: #import <sys/sysctl.h>
NSString *info = nil;
// ID范围: [HW_MACHINE, HW_MAXID)
if (ID >= HW_MAXID) {
return info;
}
struct ctlname ctlNames[] = CTL_NAMES;
struct ctlname ctlName = ctlNames[CTL_HW];
// 硬件信息标识列表
struct ctlname names[] = CTL_HW_NAMES;
struct ctlname name = names[ID];
// 拼接信息名称 (例如: hw.product)
char *infoName = malloc(64);
strcat(infoName, ctlName.ctl_name);
strcat(infoName, ".");
strcat(infoName, name.ctl_name);
NSLog(@"Hardware infoName: %s", infoName);
size_t size;
sysctlbyname(infoName, NULL, &size, NULL, 0);
if (name.ctl_type == CTLTYPE_STRING) {
char *var = malloc(size);
sysctlbyname(infoName, var, &size, NULL, 0);
if (var != NULL) {
info = [NSString stringWithCString:var encoding:NSUTF8StringEncoding];
}
free(var);
} else if (name.ctl_type == CTLTYPE_INT) {
int64_t var;
sysctlbyname(infoName, &var, &size, NULL, 0);
info = [NSString stringWithFormat:@"%lld", var];
}
free(infoName);
return info;
}
/// 读取系统信息
/// @param identifier 信息标识名称 (C语言字符串)
- (NSString *)readSystemInfoByName:(char *)identifier {
// 需要导入: #import <sys/sysctl.h>
size_t size;
sysctlbyname(identifier, NULL, &size, NULL, 0);
char *var = malloc(size);
sysctlbyname(identifier, var, &size, NULL, 0);
NSString *info = [NSString stringWithCString:var encoding:NSUTF8StringEncoding];
free(var);
if (var == NULL) {
return nil;
} else {
return info;
}
}
3. 获取设备型号名称的方法
/**
设备机型
@return 设备机型,如: "iPhone X"
*/
- (NSString *)deviceModelName { // 需要导入#import "sys/utsname.h"
struct utsname systemInfo;
uname(&systemInfo);
// NSString *sysname = [NSString stringWithCString:systemInfo.sysname encoding:NSASCIIStringEncoding]; // Name of OS
// NSString *nodename = [NSString stringWithCString:systemInfo.nodename encoding:NSASCIIStringEncoding]; // Name of this network node
// NSString *release = [NSString stringWithCString:systemInfo.release encoding:NSASCIIStringEncoding]; // Release level
// NSString *version = [NSString stringWithCString:systemInfo.version encoding:NSASCIIStringEncoding]; // Version level
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
// Apple设备型号总览: https://www.theiphonewiki.com/wiki/Models
NSDictionary *dic = @{
// 1. iPhone 系列
@"iPhone1,1" : @"iPhone 1G",
@"iPhone1,2" : @"iPhone 3G",
@"iPhone2,1" : @"iPhone 3GS",
@"iPhone3,1" : @"iPhone 4 (GSM)",
@"iPhone3,2" : @"iPhone 4",
@"iPhone3,3" : @"iPhone 4 (CDMA)",
@"iPhone4,1" : @"iPhone 4S",
@"iPhone5,1" : @"iPhone 5",
@"iPhone5,2" : @"iPhone 5",
@"iPhone5,3" : @"iPhone 5c",
@"iPhone5,4" : @"iPhone 5c",
@"iPhone6,1" : @"iPhone 5s",
@"iPhone6,2" : @"iPhone 5s",
@"iPhone7,1" : @"iPhone 6 Plus",
@"iPhone7,2" : @"iPhone 6",
@"iPhone8,1" : @"iPhone 6s",
@"iPhone8,2" : @"iPhone 6s Plus",
@"iPhone8,4" : @"iPhone SE",
@"iPhone9,1" : @"iPhone 7",
@"iPhone9,2" : @"iPhone 7 Plus",
@"iPhone9,3" : @"iPhone 7",
@"iPhone9,4" : @"iPhone 7 Plus",
@"iPhone10,1" : @"iPhone 8",
@"iPhone10,2" : @"iPhone 8 Plus",
@"iPhone10,3" : @"iPhone X",
@"iPhone10,4" : @"iPhone 8",
@"iPhone10,5" : @"iPhone 8 Plus",
@"iPhone10,6" : @"iPhone X",
@"iPhone11,2" : @"iPhone Xs",
@"iPhone11,4" : @"iPhone Xs Max",
@"iPhone11,6" : @"iPhone Xs Max",
@"iPhone11,8" : @"iPhone XR",
@"iPhone12,1" : @"iPhone 11",
@"iPhone12,3" : @"iPhone 11 Pro",
@"iPhone12,5" : @"iPhone 11 Pro Max",
@"iPhone12,8" : @"iPhone SE 2",
@"iPhone13,1" : @"iPhone 12 mini",
@"iPhone13,2" : @"iPhone 12",
@"iPhone13,3" : @"iPhone 12 Pro",
@"iPhone13,4" : @"iPhone 12 Pro Max",
@"iPhone14,2" : @"iPhone 13 Pro",
@"iPhone14,3" : @"iPhone 13 Pro Max",
@"iPhone14,4" : @"iPhone 13 mini",
@"iPhone14,5" : @"iPhone 13",
@"iPhone14,6" : @"iPhone SE 3",
@"iPhone14,7" : @"iPhone 14",
@"iPhone14,8" : @"iPhone 14 Plus",
@"iPhone15,2" : @"iPhone 14 Pro",
@"iPhone15,3" : @"iPhone 14 Pro Max",
// 2. iPod 系列
@"iPod1,1" : @"iPod touch 1",
@"iPod2,1" : @"iPod touch 2",
@"iPod3,1" : @"iPod touch 3",
@"iPod4,1" : @"iPod touch 4",
@"iPod5,1" : @"iPod touch 5",
@"iPod7,1" : @"iPod touch 6",
@"iPod9,1" : @"iPod touch 7",
// 3. iPad 系列
// iPad
@"iPad1,1" : @"iPad 1",
@"iPad2,1" : @"iPad 2 (WiFi)",
@"iPad2,2" : @"iPad 2 (GSM)",
@"iPad2,3" : @"iPad 2 (CDMA)",
@"iPad2,4" : @"iPad 2",
@"iPad3,1" : @"iPad 3 (WiFi)",
@"iPad3,2" : @"iPad 3 (CDMA)",
@"iPad3,3" : @"iPad 3 (4G)",
@"iPad3,4" : @"iPad 4 (WiFi)",
@"iPad3,5" : @"iPad 4 (4G)",
@"iPad3,6" : @"iPad 4 (CDMA)",
@"iPad6,11" : @"iPad 5",
@"iPad6,12" : @"iPad 5",
@"iPad7,5" : @"iPad 6",
@"iPad7,6" : @"iPad 6",
@"iPad7,11" : @"iPad 7",
@"iPad7,12" : @"iPad 7",
@"iPad11,6" : @"iPad 8",
@"iPad11,7" : @"iPad 8",
@"iPad12,1" : @"iPad 9",
@"iPad12,2" : @"iPad 9",
// iPad Air
@"iPad4,1" : @"iPad Air",
@"iPad4,2" : @"iPad Air",
@"iPad4,3" : @"iPad Air",
@"iPad5,3" : @"iPad Air 2",
@"iPad5,4" : @"iPad Air 2",
@"iPad11,3" : @"iPad Air 3",
@"iPad11,4" : @"iPad Air 3",
@"iPad13,1" : @"iPad Air 4",
@"iPad13,2" : @"iPad Air 4",
@"iPad13,16" : @"iPad Air 5",
@"iPad13,17" : @"iPad Air 5",
// iPad Pro
@"iPad6,3" : @"iPad Pro (9.7-inch)",
@"iPad6,4" : @"iPad Pro (9.7-inch)",
@"iPad6,7" : @"iPad Pro (12.9-inch)",
@"iPad6,8" : @"iPad Pro (12.9-inch)",
@"iPad7,1" : @"iPad Pro 2 (12.9-inch)",
@"iPad7,2" : @"iPad Pro 2 (12.9-inch)",
@"iPad7,3" : @"iPad Pro (10.5-inch)",
@"iPad7,4" : @"iPad Pro (10.5-inch)",
@"iPad8,1" : @"iPad Pro (11-inch)",
@"iPad8,2" : @"iPad Pro (11-inch)",
@"iPad8,3" : @"iPad Pro (11-inch)",
@"iPad8,4" : @"iPad Pro (11-inch)",
@"iPad8,5" : @"iPad Pro 3 (12.9-inch)",
@"iPad8,6" : @"iPad Pro 3 (12.9-inch)",
@"iPad8,7" : @"iPad Pro 3 (12.9-inch)",
@"iPad8,8" : @"iPad Pro 3 (12.9-inch)",
@"iPad8,9" : @"iPad Pro 2 (11-inch)",
@"iPad8,10" : @"iPad Pro 2 (11-inch)",
@"iPad8,11" : @"iPad Pro 4 (12.9-inch)",
@"iPad8,12" : @"iPad Pro 4 (12.9-inch)",
@"iPad13,4" : @"iPad Pro 3 (11-inch)",
@"iPad13,5" : @"iPad Pro 3 (11-inch)",
@"iPad13,6" : @"iPad Pro 3 (11-inch)",
@"iPad13,7" : @"iPad Pro 3 (11-inch)",
@"iPad13,8" : @"iPad Pro 5 (12.9-inch)",
@"iPad13,9" : @"iPad Pro 5 (12.9-inch)",
@"iPad13,10" : @"iPad Pro 5 (12.9-inch)",
@"iPad13,11" : @"iPad Pro 5 (12.9-inch)",
// iPad mini
@"iPad2,5" : @"iPad mini 1 (WiFi)",
@"iPad2,6" : @"iPad mini 1 (GSM)",
@"iPad2,7" : @"iPad mini 1 (CDMA)",
@"iPad4,4" : @"iPad mini 2",
@"iPad4,5" : @"iPad mini 2",
@"iPad4,6" : @"iPad mini 2",
@"iPad4,7" : @"iPad mini 3",
@"iPad4,8" : @"iPad mini 3",
@"iPad4,9" : @"iPad mini 3",
@"iPad5,1" : @"iPad mini 4",
@"iPad5,2" : @"iPad mini 4",
@"iPad11,1" : @"iPad mini 5",
@"iPad11,2" : @"iPad mini 5",
@"iPad14,1" : @"iPad mini 6",
@"iPad14,2" : @"iPad mini 6",
// 4. Apple Watch 系列
@"Watch1,1" : @"Apple Watch 38mm",
@"Watch1,2" : @"Apple Watch 42mm",
@"Watch2,3" : @"Apple Watch Series 2 38mm",
@"Watch2,4" : @"Apple Watch Series 2 42mm",
@"Watch2,6" : @"Apple Watch Series 1 38mm",
@"Watch2,7" : @"Apple Watch Series 1 42mm",
@"Watch3,1" : @"Apple Watch Series 3 38mm",
@"Watch3,2" : @"Apple Watch Series 3 42mm",
@"Watch3,3" : @"Apple Watch Series 3 38mm",
@"Watch3,4" : @"Apple Watch Series 3 42mm",
@"Watch4,1" : @"Apple Watch Series 4 40mm",
@"Watch4,2" : @"Apple Watch Series 4 44mm",
@"Watch4,3" : @"Apple Watch Series 4 40mm",
@"Watch4,4" : @"Apple Watch Series 4 44mm",
@"Watch5,1" : @"Apple Watch Series 5 40mm",
@"Watch5,2" : @"Apple Watch Series 5 44mm",
@"Watch5,3" : @"Apple Watch Series 5 40mm",
@"Watch5,4" : @"Apple Watch Series 5 44mm",
@"Watch5,9" : @"Apple Watch SE 40mm",
@"Watch5,10" : @"Apple Watch SE 44mm",
@"Watch5,11" : @"Apple Watch SE 40mm",
@"Watch5,12" : @"Apple Watch SE 44mm",
@"Watch6,1" : @"Apple Watch Series 6 40mm",
@"Watch6,2" : @"Apple Watch Series 6 44mm",
@"Watch6,3" : @"Apple Watch Series 6 40mm",
@"Watch6,4" : @"Apple Watch Series 6 44mm",
@"Watch6,6" : @"Apple Watch Series 7 41mm",
@"Watch6,7" : @"Apple Watch Series 7 45mm",
@"Watch6,8" : @"Apple Watch Series 7 41mm",
@"Watch6,9" : @"Apple Watch Series 7 45mm",
// 5. Apple TV 系列
@"AppleTV1,1" : @"Apple TV 1",
@"AppleTV2,1" : @"Apple TV 2",
@"AppleTV3,1" : @"Apple TV 3",
@"AppleTV3,2" : @"Apple TV 3",
@"AppleTV5,3" : @"Apple TV 4",
@"AppleTV6,2" : @"Apple TV 4K",
@"AppleTV11,1" : @"Apple TV 4K 2",
// 6. AirPods 系列
@"AirPods1,1" : @"AirPods 1",
@"AirPods1,2" : @"AirPods 2",
@"AirPods2,1" : @"AirPods 2",
@"AirPods1,3" : @"AirPods 3",
@"Audio2,1" : @"AirPods 3",
@"AirPods2,2" : @"AirPods Pro",
@"AirPodsPro1,1" : @"AirPods Pro",
@"iProd8,1" : @"AirPods Pro",
@"iProd8,6" : @"AirPods Max",
@"AirPodsMax1,1" : @"AirPods Max",
// 7. HomePod 系列
@"AudioAccessory1,1" : @"HomePod",
@"AudioAccessory1,2" : @"HomePod",
// 8. Apple Pencil 系列 (暂时未知)
// @"Unknown" : @"Apple Pencil",
// @"Unknown" : @"Apple Pencil 2",
// 9. Smart Keyboard 系列 (暂时未知)
// @"Unknown" : @"Smart Keyboard",
// @"Unknown" : @"Smart Keyboard Folio",
// 10. Siri Remote 系列 (暂时未知)
// @"Unknown" : @"Siri Remote",
// @"Unknown" : @"Siri Remote 2",
// @"Unknown" : @"Siri Remote 3",
// 11. AirTag
@"AirTag1,1" : @"AirTag",
// 模拟器
@"i386" : @"Simulator x86",
@"x86_64" : @"Simulator x64",
};
NSString *name = dic[deviceModel];
if (!name) {
name = deviceModel;
}
return name;
}
4. sysctl() 相关知识
sysctl()
和sysctlbyname()
都是标准的UNIX函数,用于获取操作系统有关内核和硬件的详细信息。
sysctl
命令用于在内核运行时动态地修改内核的运行参数,即对内核参数进行设置。可用的内核参数在目录/proc/sys
中,这些内核参数的设置都是文件。
打印当前所有可用内核参数
sysctl -a
在Mac的Terminal中执行该命令,会打印当前所有可用内核参数的变量名和值。从指定的文件加载系统内核参数
sysctl -p /xxx/xxx.xxx
如果-p
后不指定文件路径,则从/etc/sysctl.conf
中加载。读取指定内核参数的值
sysctl kern.osversion
kern.osversion
为内核参数的变量名。设置指定内核参数的值
sysctl -w kern.maxfiles=5000
kern.maxfiles
为内核参数的变量名,用variable=value
的方式设置。通过该方式设置内核参数后,如果系统重启,则所设置的值会丢失。如果想永久保留配置,则可以通过修改/etc/sysctl.conf
文件来设置内核参数。