超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)
程序员文章站
2023-12-17 16:09:04
前言
在开发中,有的时候为了统计用户信息、下发广告,服务器端往往需要手机用户设备及app的各种信息,这些信息的获取可以根据不同的设备或者app、系统版本来提供不同的功能或...
前言
在开发中,有的时候为了统计用户信息、下发广告,服务器端往往需要手机用户设备及app的各种信息,这些信息的获取可以根据不同的设备或者app、系统版本来提供不同的功能或更好的用户体验,或者让开发者能更好的分析用户的问题原因。
下面讲述一下各种信息的获取方式:
点击下载以上展示效果的github源码 ,大家也可以 本地下载
一行代码就搞定的统一来!
// 这个方法后面会列出来 nsstring *devicename = [self getdevicename]; nslog(@"设备型号-->%@", devicename); nsstring *iphonename = [uidevice currentdevice].name; nslog(@"iphone名称-->%@", iphonename); nsstring *appverion = [[[nsbundle mainbundle] infodictionary] objectforkey:@"cfbundleshortversionstring"]; nslog(@"app版本号-->%@", appverion); cgfloat batterylevel = [[uidevice currentdevice] batterylevel]; nslog(@"电池电量-->%f", batterylevel); nsstring *localizedmodel = [uidevice currentdevice].localizedmodel; nslog(@"localizedmodel-->%@", localizedmodel); nsstring *systemname = [uidevice currentdevice].systemname; nslog(@"当前系统名称-->%@", systemname); nsstring *systemversion = [uidevice currentdevice].systemversion; nslog(@"当前系统版本号-->%@", systemversion); struct utsname systeminfo; uname(&systeminfo); nsstring *device_model = [nsstring stringwithcstring:systeminfo.machine encoding:nsutf8stringencoding]; nslog(@"device_model-->%@", device_model); // 这个方法后面会单独列出 nsstring *macaddress = [self getmacaddress]; nslog(@"macaddress-->%@", macaddress); // 这个方法后面会单独列出 nsstring *deviceip = [self getdeviceipaddresses]; nslog(@"deviceip-->%@", deviceip); // 设备上次重启的时间 nstimeinterval time = [[nsprocessinfo processinfo] systemuptime]; nsdate *lastrestartdate = [[nsdate alloc] initwithtimeintervalsincenow:(0 - time)];
广告位标识符:在同一个设备上的所有app都会取到相同的值,是苹果专门给各广告提供商用来追踪用户而设的,用户可以在 设置|隐私|广告追踪里重置此id的值,或限制此id的使用,故此id有可能会取不到值,但好在apple默认是允许追踪的,而且一般用户都不知道有这么个设置,所以基本上用来监测推广效果,是戳戳有余了
nsstring *idfa = [[[asidentifiermanager sharedmanager] advertisingidentifier] uuidstring]; nslog(@"广告位标识符idfa-->%@", idfa);
uuid是universally unique identifier的缩写,中文意思是通用唯一识别码。它是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过*控制端来做辨识资讯的指 定。这样,每个人都可以建立不与其它人冲突的 uuid。在此情况下,就不需考虑数据库建立时的名称重复问题。苹果公司建议使用uuid为应用生成唯一标识字符串。
nsstring *uuid = [[[uidevice currentdevice] identifierforvendor] uuidstring]; nslog(@"唯一识别码uuid-->%@", uuid);
获取设备型号
// 获取设备型号然后手动转化为对应名称 - (nsstring *)getdevicename { // 需要#import "sys/utsname.h" #warning 题主呕心沥血总结!!最全面!亲测!全网独此一份!! struct utsname systeminfo; uname(&systeminfo); nsstring *devicestring = [nsstring stringwithcstring:systeminfo.machine encoding:nsutf8stringencoding]; if ([devicestring isequaltostring:@"iphone3,1"]) return @"iphone 4"; if ([devicestring isequaltostring:@"iphone3,2"]) return @"iphone 4"; if ([devicestring isequaltostring:@"iphone3,3"]) return @"iphone 4"; if ([devicestring isequaltostring:@"iphone4,1"]) return @"iphone 4s"; if ([devicestring isequaltostring:@"iphone5,1"]) return @"iphone 5"; if ([devicestring isequaltostring:@"iphone5,2"]) return @"iphone 5 (gsm+cdma)"; if ([devicestring isequaltostring:@"iphone5,3"]) return @"iphone 5c (gsm)"; if ([devicestring isequaltostring:@"iphone5,4"]) return @"iphone 5c (gsm+cdma)"; if ([devicestring isequaltostring:@"iphone6,1"]) return @"iphone 5s (gsm)"; if ([devicestring isequaltostring:@"iphone6,2"]) return @"iphone 5s (gsm+cdma)"; if ([devicestring isequaltostring:@"iphone7,1"]) return @"iphone 6 plus"; if ([devicestring isequaltostring:@"iphone7,2"]) return @"iphone 6"; if ([devicestring isequaltostring:@"iphone8,1"]) return @"iphone 6s"; if ([devicestring isequaltostring:@"iphone8,2"]) return @"iphone 6s plus"; if ([devicestring isequaltostring:@"iphone8,4"]) return @"iphone se"; // 日行两款手机型号均为日本独占,可能使用索尼felica支付方案而不是苹果支付 if ([devicestring isequaltostring:@"iphone9,1"]) return @"国行、日版、港行iphone 7"; if ([devicestring isequaltostring:@"iphone9,2"]) return @"港行、国行iphone 7 plus"; if ([devicestring isequaltostring:@"iphone9,3"]) return @"美版、台版iphone 7"; if ([devicestring isequaltostring:@"iphone9,4"]) return @"美版、台版iphone 7 plus"; if ([devicestring isequaltostring:@"iphone10,1"]) return @"国行(a1863)、日行(a1906)iphone 8"; if ([devicestring isequaltostring:@"iphone10,4"]) return @"美版(global/a1905)iphone 8"; if ([devicestring isequaltostring:@"iphone10,2"]) return @"国行(a1864)、日行(a1898)iphone 8 plus"; if ([devicestring isequaltostring:@"iphone10,5"]) return @"美版(global/a1897)iphone 8 plus"; if ([devicestring isequaltostring:@"iphone10,3"]) return @"国行(a1865)、日行(a1902)iphone x"; if ([devicestring isequaltostring:@"iphone10,6"]) return @"美版(global/a1901)iphone x"; if ([devicestring isequaltostring:@"ipod1,1"]) return @"ipod touch 1g"; if ([devicestring isequaltostring:@"ipod2,1"]) return @"ipod touch 2g"; if ([devicestring isequaltostring:@"ipod3,1"]) return @"ipod touch 3g"; if ([devicestring isequaltostring:@"ipod4,1"]) return @"ipod touch 4g"; if ([devicestring isequaltostring:@"ipod5,1"]) return @"ipod touch (5 gen)"; if ([devicestring isequaltostring:@"ipad1,1"]) return @"ipad"; if ([devicestring isequaltostring:@"ipad1,2"]) return @"ipad 3g"; if ([devicestring isequaltostring:@"ipad2,1"]) return @"ipad 2 (wifi)"; if ([devicestring isequaltostring:@"ipad2,2"]) return @"ipad 2"; if ([devicestring isequaltostring:@"ipad2,3"]) return @"ipad 2 (cdma)"; if ([devicestring isequaltostring:@"ipad2,4"]) return @"ipad 2"; if ([devicestring isequaltostring:@"ipad2,5"]) return @"ipad mini (wifi)"; if ([devicestring isequaltostring:@"ipad2,6"]) return @"ipad mini"; if ([devicestring isequaltostring:@"ipad2,7"]) return @"ipad mini (gsm+cdma)"; if ([devicestring isequaltostring:@"ipad3,1"]) return @"ipad 3 (wifi)"; if ([devicestring isequaltostring:@"ipad3,2"]) return @"ipad 3 (gsm+cdma)"; if ([devicestring isequaltostring:@"ipad3,3"]) return @"ipad 3"; if ([devicestring isequaltostring:@"ipad3,4"]) return @"ipad 4 (wifi)"; if ([devicestring isequaltostring:@"ipad3,5"]) return @"ipad 4"; if ([devicestring isequaltostring:@"ipad3,6"]) return @"ipad 4 (gsm+cdma)"; if ([devicestring isequaltostring:@"ipad4,1"]) return @"ipad air (wifi)"; if ([devicestring isequaltostring:@"ipad4,2"]) return @"ipad air (cellular)"; if ([devicestring isequaltostring:@"ipad4,4"]) return @"ipad mini 2 (wifi)"; if ([devicestring isequaltostring:@"ipad4,5"]) return @"ipad mini 2 (cellular)"; if ([devicestring isequaltostring:@"ipad4,6"]) return @"ipad mini 2"; if ([devicestring isequaltostring:@"ipad4,7"]) return @"ipad mini 3"; if ([devicestring isequaltostring:@"ipad4,8"]) return @"ipad mini 3"; if ([devicestring isequaltostring:@"ipad4,9"]) return @"ipad mini 3"; if ([devicestring isequaltostring:@"ipad5,1"]) return @"ipad mini 4 (wifi)"; if ([devicestring isequaltostring:@"ipad5,2"]) return @"ipad mini 4 (lte)"; if ([devicestring isequaltostring:@"ipad5,3"]) return @"ipad air 2"; if ([devicestring isequaltostring:@"ipad5,4"]) return @"ipad air 2"; if ([devicestring isequaltostring:@"ipad6,3"]) return @"ipad pro 9.7"; if ([devicestring isequaltostring:@"ipad6,4"]) return @"ipad pro 9.7"; if ([devicestring isequaltostring:@"ipad6,7"]) return @"ipad pro 12.9"; if ([devicestring isequaltostring:@"ipad6,8"]) return @"ipad pro 12.9"; if ([machinestring isequaltostring:@"ipad6,11"]) return @"ipad 5 (wifi)"; if ([machinestring isequaltostring:@"ipad6,12"]) return @"ipad 5 (cellular)"; if ([machinestring isequaltostring:@"ipad7,1"]) return @"ipad pro 12.9 inch 2nd gen (wifi)"; if ([machinestring isequaltostring:@"ipad7,2"]) return @"ipad pro 12.9 inch 2nd gen (cellular)"; if ([machinestring isequaltostring:@"ipad7,3"]) return @"ipad pro 10.5 inch (wifi)"; if ([machinestring isequaltostring:@"ipad7,4"]) return @"ipad pro 10.5 inch (cellular)"; if ([devicestring isequaltostring:@"appletv2,1"]) return @"apple tv 2"; if ([devicestring isequaltostring:@"appletv3,1"]) return @"apple tv 3"; if ([devicestring isequaltostring:@"appletv3,2"]) return @"apple tv 3"; if ([devicestring isequaltostring:@"appletv5,3"]) return @"apple tv 4"; if ([devicestring isequaltostring:@"i386"]) return @"simulator"; if ([devicestring isequaltostring:@"x86_64"]) return @"simulator"; return devicestring; }
获取 iphone 设备颜色/外壳颜色
#warning 该方法是私有api,上线会被拒 // 由于每款手机同一颜色具体色号不一样,如 iphone 6和 iphone se粉色的两个具体色值是不一样的,因此在这里只能返回一个色号 uidevice *device = [uidevice currentdevice]; sel selector = nsselectorfromstring(@"deviceinfoforkey:"); if (![device respondstoselector:selector]) { selector = nsselectorfromstring(@"_deviceinfoforkey:"); } if ([device respondstoselector:selector]) { // 消除警告“performselector may cause a leak because its selector is unknown” imp imp = [device methodforselector:selector]; nsstring * (*func)(id, sel, nsstring *) = (void *)imp; nsstring *devicecolor = func(device, selector, @"devicecolor"); nsstring *deviceenclosurecolor = func(device, selector, @"deviceenclosurecolor"); nslog(@"devicecolor --> %@ \n @"deviceenclosurecolor --> %@ ", devicecolor, deviceenclosurecolor); }
mac 地址
- (nsstring *)getmacaddress { 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. error!/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 uppercasestring]; }
ip地址
- (nsstring *)getdeviceipaddresses { int sockfd = socket(af_inet, sock_dgram, 0); nsmutablearray *ips = [nsmutablearray array]; int buffersize = 4096; struct ifconf ifc; char buffer[buffersize], *ptr, lastname[ifnamsiz], *cptr; struct ifreq *ifr, ifrcopy; ifc.ifc_len = buffersize; ifc.ifc_buf = buffer; if (ioctl(sockfd, siocgifconf, &ifc) >= 0){ for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){ ifr = (struct ifreq *)ptr; int len = sizeof(struct sockaddr); if (ifr->ifr_addr.sa_len > len) { len = ifr->ifr_addr.sa_len; } ptr += sizeof(ifr->ifr_name) + len; if (ifr->ifr_addr.sa_family != af_inet) continue; if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != null) *cptr = 0; if (strncmp(lastname, ifr->ifr_name, ifnamsiz) == 0) continue; memcpy(lastname, ifr->ifr_name, ifnamsiz); ifrcopy = *ifr; ioctl(sockfd, siocgifflags, &ifrcopy); if ((ifrcopy.ifr_flags & iff_up) == 0) continue; nsstring *ip = [nsstring stringwithformat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)]; [ips addobject:ip]; } } close(sockfd); nsstring *deviceip = @""; for (int i=0; i < ips.count; i++) { if (ips.count > 0) { deviceip = [nsstring stringwithformat:@"%@",ips.lastobject]; } } return deviceip; }
cpu
// cpu总数目 - (nsuinteger)getcpucount { return [nsprocessinfo processinfo].activeprocessorcount; } // 已使用的cpu比例 - (float)getcpuusage { float cpu = 0; nsarray *cpus = [self getpercpuusage]; if (cpus.count == 0) return -1; for (nsnumber *n in cpus) { cpu += n.floatvalue; } return cpu; } // 获取每个cpu的使用比例 - (nsarray *)getpercpuusage { processor_info_array_t _cpuinfo, _prevcpuinfo = nil; mach_msg_type_number_t _numcpuinfo, _numprevcpuinfo = 0; unsigned _numcpus; nslock *_cpuusagelock; int _mib[2u] = { ctl_hw, hw_ncpu }; size_t _sizeofnumcpus = sizeof(_numcpus); int _status = sysctl(_mib, 2u, &_numcpus, &_sizeofnumcpus, null, 0u); if (_status) _numcpus = 1; _cpuusagelock = [[nslock alloc] init]; natural_t _numcpusu = 0u; kern_return_t err = host_processor_info(mach_host_self(), processor_cpu_load_info, &_numcpusu, &_cpuinfo, &_numcpuinfo); if (err == kern_success) { [_cpuusagelock lock]; nsmutablearray *cpus = [nsmutablearray new]; for (unsigned i = 0u; i < _numcpus; ++i) { float32 _inuse, _total; if (_prevcpuinfo) { _inuse = ( (_cpuinfo[(cpu_state_max * i) + cpu_state_user] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_user]) + (_cpuinfo[(cpu_state_max * i) + cpu_state_system] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_system]) + (_cpuinfo[(cpu_state_max * i) + cpu_state_nice] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_nice]) ); _total = _inuse + (_cpuinfo[(cpu_state_max * i) + cpu_state_idle] - _prevcpuinfo[(cpu_state_max * i) + cpu_state_idle]); } else { _inuse = _cpuinfo[(cpu_state_max * i) + cpu_state_user] + _cpuinfo[(cpu_state_max * i) + cpu_state_system] + _cpuinfo[(cpu_state_max * i) + cpu_state_nice]; _total = _inuse + _cpuinfo[(cpu_state_max * i) + cpu_state_idle]; } [cpus addobject:@(_inuse / _total)]; } [_cpuusagelock unlock]; if (_prevcpuinfo) { size_t prevcpuinfosize = sizeof(integer_t) * _numprevcpuinfo; vm_deallocate(mach_task_self(), (vm_address_t)_prevcpuinfo, prevcpuinfosize); } return cpus; } else { return nil; } }
disk磁盘空间
// 获取磁盘总空间 - (int64_t)gettotaldiskspace { nserror *error = nil; nsdictionary *attrs = [[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:&error]; if (error) return -1; int64_t space = [[attrs objectforkey:nsfilesystemsize] longlongvalue]; if (space < 0) space = -1; return space; } // 获取未使用的磁盘空间 - (int64_t)getfreediskspace { nserror *error = nil; nsdictionary *attrs = [[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:&error]; if (error) return -1; int64_t space = [[attrs objectforkey:nsfilesystemfreesize] longlongvalue]; if (space < 0) space = -1; return space; } // 获取已使用的磁盘空间 - (int64_t)getuseddiskspace { int64_t totaldisk = [self gettotaldiskspace]; int64_t freedisk = [self getfreediskspace]; if (totaldisk < 0 || freedisk < 0) return -1; int64_t useddisk = totaldisk - freedisk; if (useddisk < 0) useddisk = -1; return useddisk; }
memory内存相关数据
// 系统总内存空间 - (int64_t)gettotalmemory { int64_t totalmemory = [[nsprocessinfo processinfo] physicalmemory]; if (totalmemory < -1) totalmemory = -1; return totalmemory; } // 活跃的内存,正在使用或者很短时间内被使用过 - (int64_t)getactivememory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return vm_stat.active_count * page_size; } // 最近使用过,但是目前处于不活跃状态的内存 - (int64_t)getinactivememory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return vm_stat.inactive_count * page_size; } // 空闲的内存空间 - (int64_t)getfreememory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return vm_stat.free_count * page_size; } // 已使用的内存空间 - (int64_t)getusedmemory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return page_size * (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count); } // 用来存放内核和数据结构的内存,framework、用户级别的应用无法分配 - (int64_t)getwiredmemory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return vm_stat.wire_count * page_size; } // 可释放的内存空间:内存吃紧自动释放,针对大对象存放所需的大块内存空间 - (int64_t)getpurgablememory { 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 page_size; vm_statistics_data_t vm_stat; kern_return_t kern; kern = host_page_size(host_port, &page_size); if (kern != kern_success) return -1; kern = host_statistics(host_port, host_vm_info, (host_info_t)&vm_stat, &host_size); if (kern != kern_success) return -1; return vm_stat.purgeable_count * page_size; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。