获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)
1.获取电池电量(一般用百分数表示,大家自行处理就好)
1
2
3
4
|
-(CGFloat)getBatteryQuantity { return [[UIDevice currentDevice] batteryLevel]; } |
2.获取电池状态(UIDeviceBatteryState为枚举类型)
1
2
3
4
|
-(UIDeviceBatteryState)getBatteryStauts { return [UIDevice currentDevice].batteryState; } |
3.获取总内存大小
1
2
3
4
|
-(long long)getTotalMemorySize { return [NSProcessInfo processInfo].physicalMemory; } |
4.获取当前可用内存
1
2
3
4
5
6
7
8
9
10
11
|
-(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.获取已使用内存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
- (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.获取总磁盘容量
1
2
3
4
5
6
7
8
9
10
11
|
include <sys mount.h= "" > -(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; }</sys> |
7.获取可用磁盘容量
1
2
3
4
5
6
7
8
9
10
|
-(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.容量转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
-(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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
+ (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; } |
可以根据自己的需求增改。有人说也可以按照手机屏幕来判断,但5和5s/5c等手机屏幕相同尺寸则无法判断。。。我只做了iPhone的机型,所以iPad和iPod Touch的信息并没有更新到最新,也请朋友们补充。
9.IP地址
#import
和#import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
- (NSString *)deviceIPAdress { NSString *address = @ "an error occurred when obtaining ip address" ; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 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 iPhone if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@ "en0" ]) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } freeifaddrs(interfaces); return address; } |
10.当前手机连接的WIFI名称(SSID)
需要#import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
- (NSString *)getWifiName { NSString *wifiName = nil; CFArrayRef wifiInterfaces = CNCopySupportedInterfaces(); if (!wifiInterfaces) { return nil; } NSArray *interfaces = (__bridge NSArray *)wifiInterfaces; for (NSString *interfaceName in interfaces) { CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName)); if (dictRef) { NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef; wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID]; CFRelease(dictRef); } } CFRelease(wifiInterfaces); return wifiName; } |
http://www.cocoachina.com/ios/20161130/18229.html
获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)的更多相关文章
- iOS开发的另类神器:libimobiledevice开源包【类似android adb 方便获取iOS设备信息】
简介 libimobiledevice又称libiphone,是一个开源包,可以让Linux支持连接iPhone/iPod Touch等iOS设备.由于苹果官方并不支持Linux系统,但是Linux上 ...
- 获取iOS设备的内存状况(转)
转载自:http://mobile.51cto.com/iphone-285371.htm iPhone这类移动设备内存有限,而又不能使用交换区,为了不至于导致内存不足而引起运行效率降低或应用崩溃 ...
- 获取iOS设备信息的编程接口
参考资料: [1] 博客园,iOS屏幕尺寸和分辨率了解 [2] 张兴业,获取手机信息(UIDevice.NSBundle.NSLocale), CSDN
- iOS学习——获取iOS设备的各种信息
不管是在Android开发还是iOS开发过程中,有时候我们需要经常根据设备的一些状态或信息进行不同的设置和性能配置,例如横竖屏切换时,电池电量低时,内存不够时,网络切换时等等,我们在这时候需要进行一些 ...
- 获取iOS设备型号的方法总结
三种常用的办法获取iOS设备的型号: 1. [UIDevice currentDevice].model (推荐): 2. uname(struct utsname *name) ,使用此函数需要#i ...
- iOS开发-Swift获取手机设备信息(UIDevice)
使用UiDevice获取设备信息 获取设备名称 let name = UIDevice.currentDevice().name 获取设备系统名称 let systemName = UIDevice. ...
- 获取iOS设备的型号
获取iOS设备的型号 需要#import "sys/utsname.h" structutsname systemInfo; uname(&systemIn ...
- iOS开发-Object-C获取手机设备信息(UIDevice)
一.获取UiDevice设备信息 // 获取设备名称 NSString *name = [[UIDevice currentDevice] name]; // 获取设备系统名称 NSString *s ...
- Atitit.获取主板与bios序列号获取硬件设备信息 Wmi wmic 的作用
Atitit.获取主板与bios序列号获取硬件设备信息 Wmi wmic 的作用 1 获取硬件核心基础核心基础Wmi1 2 其他资料2 3 Wmic WMI 命令行接口2 4 Atitit.获取主板 ...
随机推荐
- MapReduce 中的Map后,sort不能对中文的key排序
今天写了一个用mapreduce求平均分的程序,结果是出来了,可是没有按照“学生名字”进行排序,如果是英文名字的话,结果是排好序的. 代码如下: package com.pro.bq; import ...
- Linux3.4内核的基本配置和编译
转载自:http://www.embedu.org/Column/Column634.htm 作者:李昕,华清远见研发中心讲师. 了解Linux3.4内核的特性及新增功能,掌握Linux内核的编译过程 ...
- 1002: A+B for Input-Output Practice (II)
问题描述: http://acm.wust.edu.cn/problem.php?id=1002&soj=0 代码实现: import java.util.Scanner; public cl ...
- poj 2777 Count Color(线段树 区间更新)
题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释, 参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- bzoj2241: [SDOI2011]打地鼠
暴力. O(n^6)暴力卡过,72ms. 莫名其妙做这道题时感觉十分烦躁,难受,只能这样了. O(n^4)的方法是这样差分一下.判断的时候tmp=t[i][j],t[i][j]-=tmp,t[i+r] ...
- BZOJ2253: [2010 Beijing wc]纸箱堆叠
题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...
- datatable 的ajax修改参数,post可以传参处理
datatables常用参数记录 { "searchable": false, "orderabl ...
- jquery生成二维码
下载Demo js下载: jquery-1.8.3.js .qrcode.js.jquery.qrcode.js <!DOCTYPE html> <html> <head ...
- Jquery图片放大镜
一般在“在线商城.电子商务.企业产品介绍”等地方经常会看到一些图片放大镜的功能,而做这个功能一般是会用一个js包——enlarge.js(这是jquery图片放大镜的插件).Enlarge 是一个基于 ...