close
当前位置: 物联网在线 > 技术文库 > ios >

获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)

最近工作上需要获取设备的一些信息,整理了一下,方便大家。

1.获取电池电量(一般用百分数表示,大家自行处理就好)

-(CGFloat)getBatteryQuantity { return [[UIDevice currentDevice] batteryLevel]; }

2.获取电池状态(UIDeviceBatteryState为枚举类型)

-(UIDeviceBatteryState)getBatteryStauts { return [UIDevice currentDevice].batteryState; }

3.获取总内存大小

-(long long)getTotalMemorySize { return [NSProcessInfo processInfo].physicalMemory; }

4.获取当前可用内存

-(long long)getAvailableMemorySize { vm_statistics_data_t vmStats; mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT; kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount); if (kernReturn != KERN_SUCCESS) { return NSNotFound; } return ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count)); }

5.获取已使用内存

- (double)getUsedMemory { task_basic_info_data_t taskInfo; mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT; kern_return_t kernReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount); if (kernReturn != KERN_SUCCESS ) { return NSNotFound; } return taskInfo.resident_size; }

6.获取总磁盘容量

include -(long long)getTotalDiskSize { struct statfs buf; unsigned long long freeSpace = -1; if (statfs("/var", &buf) >= 0) { freeSpace = (unsigned long long)(buf.f_bsize * buf.f_blocks); } return freeSpace; }

7.获取可用磁盘容量

-(long long)getAvailableDiskSize { struct statfs buf; unsigned long long freeSpace = -1; if (statfs("/var", &buf) >= 0) { freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail); } return freeSpace; }

8.容量转换

-(NSString *)fileSizeToString:(unsigned long long)fileSize { NSInteger KB = 1024; NSInteger MB = KB*KB; NSInteger GB = MB*KB; if (fileSize < 10) { return @"0 B"; }else if (fileSize < KB) { return @"< 1 KB"; }else if (fileSize < MB) { return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB]; }else if (fileSize < GB) { return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB]; }else { return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB]; } }

8.型号

#import <sys sysctl="" h=""></sys>

+ (NSString *)getCurrentDeviceModel:(UIViewController *)controller { int mib[2]; size_t len; char *machine; mib[0] = CTL_HW; mib[1] = HW_MACHINE; sysctl(mib, 2, NULL, &len, NULL, 0); machine = malloc(len); sysctl(mib, 2, machine, &len, NULL, 0); NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding]; free(machine); if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (A1332)"; if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (A1332)"; if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (A1349)"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4s (A1387/A1431)"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (A1428)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (A1429/A1442)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (A1456/A1532)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (A1507/A1516/A1526/A1529)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (A1453/A1533)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (A1457/A1518/A1528/A1530)"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus (A1522/A1524)"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6 (A1549/A1586)"; if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G (A1213)"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G (A1288)"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G (A1318)"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G (A1367)"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G (A1421/A1509)"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1G (A1219/A1337)"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (A1395)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (A1396)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (A1397)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (A1395+New Chip)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini 1G (A1432)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini 1G (A1454)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini 1G (A1455)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (A1416)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (A1403)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (A1430)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (A1458)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (A1459)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (A1460)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (A1474)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (A1475)"; if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (A1476)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G (A1489)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2G (A1490)"; if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2G (A1491)"; if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"iPhone Simulator"; return platform; }
(责任编辑:ioter)

用户喜欢...

iOS开源:Tkeyboard - 通过蓝牙,使用 Mac 的键盘输入内容到 iPhone 设备中

在咖啡馆耗费了数个周末之后,终于将这个灵光一闪的点子变成了一个实实在在可以运行的 app。同时意味着年初制定的第一个小目标达成,完成了 2017年的第一个独立 app。 在app上架的同时,...


iOS开发——多线程完成短信获取按钮倒计时

现在的APP应用中,用手机获取短信验证码是非常常见的一个功能,而往往要求的效果就是在按下获取验证码之后,验证码的按钮开始倒计时,例如30秒后重新获取。而我们如何来完成这个效果...


iOS 获取 Label 高度的正确方式

在设置 UILabel 的 Frame 高度时,不能简单的设置为字体的 font size 。否则会将字体的一部分裁剪掉。因为 UILabel 在不同的字体设置下,对 Frame 的高度要求也不一样,大多数情况下都比Font的高度...


iOS开源 - 应用信息输出微控制台

TinyConsole A tiny log console to display information while using your iOS app. Written in Swift 3. Usage Create a TinyConsoleController -Instance and pass your App-ViewController as a rootViewController parameter. TinyConsoleController(rootViewCon...


获取iOS设备唯一标示UUID

在开发过程中,我们经常会被要求获取每个设备的唯一标示,以便后台做相应的处理。我们来看看有哪些方法来获取设备的唯一标示,然后再分析下这些方法的利弊。 具体可以分为如下几种...