1.获取电池电量(一般用百分数表示,大家自行处理就好)
-(CGFloat)getBatteryQuantity{return[[UIDevicecurrentDevice] batteryLevel];}
2.获取电池状态(UIDeviceBatteryState为枚举类型)
-(UIDeviceBatteryState)getBatteryStauts{return[UIDevicecurrentDevice].batteryState;}
3.获取总内存大小
-(longlong)getTotalMemorySize{return[NSProcessInfoprocessInfo].physicalMemory;}
4.获取当前可用内存
-(longlong)getAvailableMemorySize{vm_statistics_data_tvmStats;mach_msg_type_number_tinfoCount = HOST_VM_INFO_COUNT;kern_return_tkernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);if(kernReturn != KERN_SUCCESS) {returnNSNotFound; }return((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));}
5.获取已使用内存
- (double)getUsedMemory{task_basic_info_data_ttaskInfo;mach_msg_type_number_tinfoCount = TASK_BASIC_INFO_COUNT;kern_return_tkernReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);if(kernReturn != KERN_SUCCESS ) {returnNSNotFound; }returntaskInfo.resident_size;}
6.获取总磁盘容量
include -(longlong)getTotalDiskSize{structstatfs buf;unsignedlonglongfreeSpace = -1;if(statfs("/var", &buf) >=0) { freeSpace = (unsignedlonglong)(buf.f_bsize * buf.f_blocks); }returnfreeSpace;}
7.获取可用磁盘容量
-(longlong)getAvailableDiskSize{structstatfs buf;unsignedlonglongfreeSpace = -1;if(statfs("/var", &buf) >=0) { freeSpace = (unsignedlonglong)(buf.f_bsize * buf.f_bavail); }returnfreeSpace;}
8.容量转换
-(NSString*)fileSizeToString:(unsignedlonglong)fileSize{NSIntegerKB =1024;NSIntegerMB = KB*KB;NSIntegerGB = MB*KB;if(fileSize <10) {return@"0 B"; }elseif(fileSize < KB) {return@"< 1 KB"; }elseif(fileSize < MB) {return[NSStringstringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB]; }elseif(fileSize < GB) {return[NSStringstringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB]; }else{return[NSStringstringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB]; }}
8.型号
#import
+ (NSString *)getCurrentDeviceModel:(UIViewController *)controller{intmib[2];size_tlen;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";returnplatform;}
可以根据自己的需求增改。有人说也可以按照手机屏幕来判断,但5和5s/5c等手机屏幕相同尺寸则无法判断。。。我只做了iPhone的机型,所以iPad和iPod Touch的信息并没有更新到最新,也请朋友们补充。
9.IP地址
#import <ifaddrs.h>和#import<arpa/inet.h>
- (NSString*)deviceIPAdress {NSString*address =@"an error occurred when obtaining ip address";structifaddrs *interfaces =NULL;structifaddrs *temp_addr =NULL;intsuccess =0; success = getifaddrs(&interfaces);if(success ==0) {// 0 表示获取成功temp_addr = interfaces;while(temp_addr !=NULL) {if( temp_addr->ifa_addr->sa_family == AF_INET) {// Check if interface is en0 which is the wifi connection on the iPhoneif([[NSStringstringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {// Get NSString from C Stringaddress = [NSStringstringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } freeifaddrs(interfaces);returnaddress;}
10.当前手机连接的WIFI名称(SSID)
需要#import<SystemConfiguration/CaptiveNetwork.h>
- (NSString*)getWifiName{NSString*wifiName =nil;CFArrayRefwifiInterfaces = CNCopySupportedInterfaces();if(!wifiInterfaces) {returnnil; }NSArray*interfaces = (__bridgeNSArray*)wifiInterfaces;for(NSString*interfaceNameininterfaces) {CFDictionaryRefdictRef = CNCopyCurrentNetworkInfo((__bridgeCFStringRef)(interfaceName));if(dictRef) {NSDictionary*networkInfo = (__bridgeNSDictionary*)dictRef; wifiName = [networkInfo objectForKey:(__bridgeNSString*)kCNNetworkInfoKeySSID];CFRelease(dictRef); } }CFRelease(wifiInterfaces);returnwifiName;}
11.当前手机系統版本
[[[UIDevice currentDevice] systemVersion] floatValue] ;
原文链接://www.greatytc.com/p/c3499d8e9a72 [文/CrazySteven(简书作者)]