UIDevice类展示了一些关键的特定于设备的属性,包括使用的iPhone ,Ipad或iPod Touch型号、设备名称、以及OS名称和版本。
他是一种一站式解决方案,用于提取出某些系统详细信息。每个方法都是一个实例方法,他们是使用UIDevice单例通过[UIDevice currentDevice]调用的。
可以通过UIDevice获取的系统信息包括下面的这些项。
- 1.systemName:它用于返回当前使用的操作系统的名称。对于目前这一代IOS设备,在平台上只运行一种OS:iPhone OS.
- 2.systemVersion:这个值将列出单元上目前安装的固定版本。
- 3.model:iPhone 型号返回一个描述其平台的字符串,既iPhone、iPad和iPod Touch.如果将IOS扩展到新设备上,将使用额外的字符串描述这些型号。localizedModel提供了该属性的本地化版本。
- 4.userInterfaceIdiom:这个属性表示当前设备上使用的界面风格,既iPhone或iPad.当Apple提供另外的平台风格时,可能会引入其他的用语。
- 5.name:这个字符串由用户指定的iPhone名称,这个名称也用于创建设备的本地主机名。
UIDevice *device = [UIDevice currentDevice];
NSLog(@"systemName :%@",device.systemName);
NSLog(@"systemVersion :%@",device.systemVersion);
NSLog(@"model :%@",device.model);
NSLog(@"localizedModel:%@",device.localizedModel);
NSLog(@"userInterfaceIdiom:%d",device.userInterfaceIdiom);
NSLog(@"name:%@",device.name);
输出:
systemName :iPhone OS
systemVersion :8.1.2
model :iPhone
localizedModel:iPhone
userInterfaceIdiom:0
name:“xxxx”的 iPhone
主要用法:获取设备唯一标识
思路:通过keychain缓存,没有的话生成一串随机值。
UIDevice (Extention)
- (NSString *)zl_deviceId {
static NSString *deviceId;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
deviceId = [KeychainWrapper keychainStringFromMatchingIdentifier:@"zl_deviceId"];
if (!deviceId) {
deviceId = [[self identifierForVendor] UUIDString];
if (deviceId) {
[KeychainWrapper createKeychainValue:deviceId forIdentifier:@"zl_deviceId"];
} else {
NSAssert(0, @"Device ID not found");
deviceId = [self randomDeviceId];
}
}
});
return deviceId;
}
- (void)removeKeychainDeviceId {
[KeychainWrapper deleteItemFromKeychainWithIdentifier:@"zl_deviceId"];
}
- (NSString *)randomDeviceId {
生成一个随机字符串
return uniqueId;
}
主要用法:
#import <UIKit/UIKit.h>
@interface UIDevice (Extention)
/// 平台类型
+ (NSString *)platform;
/// 平台字符串
+ (NSString *)platformString;
/// 系统的版本号
+ (NSString *)systemVersion;
/// mac地址
+ (NSString *)macAddress;
/// 获取手机屏幕分辨率
+ (NSString *)screenPixel;
///获取机型字符串
+ (NSString*)deviceString;
/// CPU数目
+ (NSUInteger)cpuNumber;
/// CPU频率
+ (NSUInteger)cpuFrequency;
///指令集
+ (NSString *)instructionSetModelString;
/// 总线频率
+ (NSUInteger)busFrequency;
/// 内存大小
+ (NSUInteger)ramSize;
/// 获取手机内存总量, 返回的是字节数
+ (long long)totalMemoryBytes;
/// 获取手机可用内存, 返回的是字节数
+ (NSUInteger)freeMemoryBytes;
/// 获取手机硬盘空闲空间, 返回的是字节数
+ (long long)freeDiskSpaceBytes;
/// 获取手机硬盘总空间, 返回的是字节数
+ (long long)totalDiskSpaceBytes;
/// 判断当前系统是否有摄像头
+ (BOOL)hasCamera;
///判断手机是否越狱(方法待完善)
+ (BOOL)isJailBreak;
///获取设备时间
+ (uint64_t)tickCount;
///电池电量
+ (CGFloat)getBatteryQuantity;
//是否在充电
+ (BOOL)isCharging;
///SIM卡信息
+ (NSDictionary *)simCardInfo;
//当前wifi的SSID
+ (NSString *)currentWifiSSID;
///是否为分型模式
+ (BOOL)isFlightMode;
@end
@implementation UIDevice (Extention)
+ (NSString *)platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
return platform;
}
+ (NSString *)platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)";
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (CDMA)";
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS MAX";
if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (CDMA)";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (CDMA)";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";
if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (GSM)";
if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (CDMA)";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini Retina (WiFi)";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini Retina (Cellular)";
if ([platform isEqualToString:@"iPad4,7"]) return @"iPad Mini 3 (WiFi)";
if ([platform isEqualToString:@"iPad4,8"]) return @"iPad Mini 3 (Cellular)";
if ([platform isEqualToString:@"iPad4,9"]) return @"iPad Mini 3 (Cellular)";
if ([platform isEqualToString:@"iPad5,1"]) return @"iPad Mini 4 (WiFi)";
if ([platform isEqualToString:@"iPad5,2"]) return @"iPad Mini 4 (Cellular)";
if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)";
if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)";
if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro 9.7-inch (WiFi)";
if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7-inch (Cellular)";
if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9-inch (WiFi)";
if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9-inch (Cellular)";
if ([platform isEqualToString:@"iPad6,11"]) return @"iPad 5 (WiFi)";
if ([platform isEqualToString:@"iPad6,12"]) return @"iPad 5 (Cellular)";
if ([platform isEqualToString:@"iPad7,1"]) return @"iPad Pro 12.9-inch 2 (WiFi)";
if ([platform isEqualToString:@"iPad7,2"]) return @"iPad Pro 12.9-inch 2 (Cellular)";
if ([platform isEqualToString:@"iPad7,3"]) return @"iPad Pro 10.5-inch (WiFi)";
if ([platform isEqualToString:@"iPad7,4"]) return @"iPad Pro 10.5-inch (Cellular)";
if ([platform isEqualToString:@"iPad7,5"]) return @"iPad 6 (WiFi)";
if ([platform isEqualToString:@"iPad7,6"]) return @"iPad 6 (Cellular)";
if ([platform isEqualToString:@"i386"]) return [UIDevice currentDevice].model;
if ([platform isEqualToString:@"x86_64"]) return [UIDevice currentDevice].model;
return platform;
}
//指令集模式
+ (NSString *)instructionSetModelString{
NSString *platform = [self platform];
NSArray *armV6Array = @[@"iPhone1,1",
@"iPhone1,2",
@"iPod1,1",
@"iPod2,1"];
NSArray *armV7Array = @[@"iPhone2,1",
@"iPhone3,1",
@"iPhone3,3",
@"iPhone4,1",
@"iPod3,1",
@"iPod4,1",
@"iPod5,1",
@"iPad1,1",
@"iPad2,1",
@"iPad2,2",
@"iPad2,3",
@"iPad2,4",
@"iPad2,5",
@"iPad2,6",
@"iPad2,7",
@"iPad3,1",
@"iPad3,2",
@"iPad3,3"];
NSArray *armV7sArray = @[@"iPhone5,1",
@"iPhone5,2",
@"iPhone5,3",
@"iPhone5,4",
@"iPod7,1",
@"iPad3,4",
@"iPad3,5",
@"iPad3,6"];
NSArray *armV8Array = @[@"iPhone6,1",
@"iPhone6,2",
@"iPhone7,1",
@"iPhone7,2",
@"iPhone8,1",
@"iPhone8,2",
@"iPhone8,4",
@"iPhone9,1",
@"iPhone9,2",
@"iPhone9,3",
@"iPhone9,4",
@"iPhone10,1",
@"iPhone10,2",
@"iPhone10,3",
@"iPhone10,4",
@"iPhone10,5",
@"iPhone10,6",
@"iPhone11,2",
@"iPhone11,4",
@"iPhone11,8",
@"iPad4,1",
@"iPad4,2",
@"iPad4,3",
@"iPad4,4",
@"iPad4,5",
@"iPad4,7",
@"iPad4,8",
@"iPad4,9",
@"iPad5,1",
@"iPad5,2",
@"iPad5,3",
@"iPad5,4",
@"iPad6,3",
@"iPad6,4",
@"iPad6,7",
@"iPad6,8",
@"iPad6,11",
@"iPad6,12",
@"iPad7,1",
@"iPad7,2",
@"iPad7,3",
@"iPad7,4",
@"iPad7,5",
@"iPad7,6"
];
if ([armV6Array containsObject:platform]) {
return @"ARMV6";
} else if([armV7Array containsObject:platform]) {
return @"ARMV7";
} else if ([armV7sArray containsObject:platform]){
return @"ARMV7s";
} else if([armV8Array containsObject:platform]) {
return @"ARM64";
}
return nil;
}
// 获取手机屏幕分辨率
+ (NSString *)screenPixel
{
return [NSString stringWithFormat:@"%dx%d", (int)[[UIScreen mainScreen] currentMode].size.width, (int)[[UIScreen mainScreen] currentMode].size.height];
}
+ (NSString *)macAddress
{
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if((mib[5] = if_nametoindex("en0")) == 0) {
printf("Error: if_nametoindex error\n");
return NULL;
}
if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1\n");
return NULL;
}
if((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. Rrror!\n");
return NULL;
}
if(sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return outstring;
}
///获取机型字符串
+ (NSString*)deviceString {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
return deviceString;
}
+ (NSString *)systemVersion
{
return [[UIDevice currentDevice] systemVersion];
}
+ (BOOL)hasCamera
{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
+ (NSUInteger)getSysInfo:(uint)typeSpecifier
{
size_t size = sizeof(int);
int result;
int mib[2] = {CTL_HW, typeSpecifier};
sysctl(mib, 2, &result, &size, NULL, 0);
return (NSUInteger)result;
}
+ (NSUInteger)cpuFrequency
{
return [self getSysInfo:HW_CPU_FREQ];
}
+ (NSUInteger)busFrequency
{
return [self getSysInfo:HW_BUS_FREQ];
}
+ (NSUInteger)ramSize
{
return [self getSysInfo:HW_MEMSIZE];
}
+ (NSUInteger)cpuNumber
{
return [self getSysInfo:HW_NCPU];
}
+ (CGFloat)getBatteryQuantity
{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
return [[UIDevice currentDevice] batteryLevel];
}
+ (BOOL)isCharging {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
return [[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging;
}
+ (NSDictionary *)simCardInfo {
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
NSMutableDictionary *simInfo = [NSMutableDictionary dictionaryWithCapacity:0];
NSString *carrierName = carrier.carrierName;
NSString *mobileCountryCode = carrier.mobileCountryCode;
NSString *mobileNetworkCode = carrier.mobileNetworkCode;
BOOL allowsVOIP = carrier.allowsVOIP;
if (carrierName.length > 0) {
[simInfo setValue:carrierName forKey:@"carrierName"];
}
if (mobileCountryCode.length > 0) {
[simInfo setValue:mobileCountryCode forKey:@"mobileCountryCode"];
}
if (mobileNetworkCode.length > 0) {
[simInfo setValue:mobileNetworkCode forKey:@"mobileNetworkCode"];
}
[simInfo setValue:@(allowsVOIP).stringValue forKey:@"mobileCountryCode"];
return simInfo;
}
+ (BOOL)isFlightMode {
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
return info.currentRadioAccessTechnology == nil;
}
+ (NSString *)currentWifiSSID
{
// Does not work on the simulator.
NSString *ssid = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
if (info[@"SSID"]) {
ssid = info[@"SSID"];
}
}
return ssid;
}
#pragma mark - # 内存信息
+ (long long)totalMemoryBytes
{
return [NSProcessInfo processInfo].physicalMemory;
// return [self getSysInfo:HW_PHYSMEM];
}
+ (NSUInteger)freeMemoryBytes
{
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
return 0;
}
unsigned long mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
#pragma mark - # 硬盘信息
+ (long long)freeDiskSpaceBytes
{
struct statfs buf;
long long freespace;
freespace = 0;
if ( statfs("/private/var", &buf) >= 0 ) {
freespace = (long long)buf.f_bsize * buf.f_bfree;
}
return freespace;
}
+ (long long)totalDiskSpaceBytes
{
struct statfs buf;
long long totalspace;
totalspace = 0;
if ( statfs("/private/var", &buf) >= 0 ) {
totalspace = (long long)buf.f_bsize * buf.f_blocks;
}
return totalspace;
}
#pragma mark - 越狱
+ (BOOL)isJailBreak
{
for (int i = 0; i < sizeof(zz_jailbreak_tool_pathes) / sizeof(char*); i++) {
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:zz_jailbreak_tool_pathes[i]]]) {
NSLog(@"The device is jail broken!");
return YES;
}
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
NSLog(@"The device is jail broken!");
return YES;
}
NSLog(@"The device is NOT jail broken!");
return NO;
}
#pragma mark -
+ (uint64_t)tickCount
{
static mach_timebase_info_data_t sTimebaseInfo;
uint64_t machTime = mach_absolute_time();
// Convert to nanoseconds - if this is the first time we've run, get the timebase.
if (sTimebaseInfo.denom == 0 )
{
(void) mach_timebase_info(&sTimebaseInfo);
}
// Convert the mach time to milliseconds
uint64_t millis = ((machTime / 1000000) * sTimebaseInfo.numer) / sTimebaseInfo.denom;
return millis;
}
@end
网友评论