美文网首页iOS 开发学习成长之路iOS开发Swift编程
史上最全的iOS各种设备信息获取总结(iPhone X 详细信息

史上最全的iOS各种设备信息获取总结(iPhone X 详细信息

作者: si1ence | 来源:发表于2016-07-01 20:05 被阅读25064次

    更新至iPhone X
    新增设备颜色的获取

    为了统计用户信息、下发广告,服务器端往往需要手机用户设备及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;
    }
    

    再放一下GitHub源码

    相关文章

      网友评论

      • 天上飞的狒狒:大神年年有,今年特别多
      • 荒漠现甘泉:碉堡了,果然是史上最全👍
      • 时间shiwo9:iPhoneXR都更新了,赞
      • 达道者:if ([deviceString isEqualToString:@"iPhone11,1"]) return @"iPhone XS";
        这行代码有待商榷呢
        si1ence:@南溟之巅 恩,这个确实还没出,已经删掉了
      • d168e1d26662:楼主,什么时候出一个swift版本哈
      • 长若执念:从iOS7及更高版本往后,如果你向iOS设备请求获取MAC地址,系统将返回一个固定值“02:00:00:00:00:00”,如果你需要识别设备的 唯一性,请使用UIDevice的identifierForVendor属性。(因广告目的而需要识别设备的应用,请考虑使用 ASIdentifierManager的advertisingIdentifier属性作为替代)
      • YLAS007:你好我想问一下, if ([machineString isEqualToString:@"iPad7,4"]) return @"iPad Pro 10.5 inch (Cellular)",怎么知道 iPad7,4 就是 iPad Pro 10.5 inch 呢?有没有网站可以查询到?
      • Hello_kid:设备颜色返回1 是什么意思呢;
      • Hello_kid:static const NSString *iDeviceNameContainer[] = {
        [iPhone_1G] = @"iPhone 1G",
        [iPhone_3G] = @"iPhone 3G",
        [iPhone_3GS] = @"iPhone 3GS",
        [iPhone_4] = @"iPhone 4",
        [iPhone_4_Verizon] = @"Verizon iPhone 4",
        [iPhone_4S] = @"iPhone 4S",
        [iPhone_5_GSM] = @"iPhone 5 (GSM)",
        [iPhone_5_CDMA] = @"iPhone 5 (CDMA)",
        } ;这种写法是什么意思?
        Hello_kid:数组里每一个对象是字典嘛
        Hello_kid:@si1ence 这是一个数组啊
        si1ence:@Ruiz678 key value啊
      • 马铃薯蜀黍:iponex怎么识别
        马铃薯蜀黍:@si1ence 感谢
        si1ence:@马铃薯蜀黍 判断真机的话通过设备号,文章里面有,如果要考虑模拟器的话最好用状态栏高度判断
      • Hello_kid:您好,这个deviceToken和iemi 都代表什么?傻傻分不清
        Hello_kid:@si1ence 但是返回deviceToken的代理方法没调用, 是不是我还的配置个推送证书才好使;
        si1ence:@Ruiz678 iMei是手机背面的国际移动手机标识符,手机组装完之后会分配一个,iOS5之后不让获取了; deviceToken一般在推送的时候会用到,是Apple返给你的,可以看下APPdelegate里面代码,这个在一定情况下会变化
      • Minoz_min:if ([machineString isEqualToString:@"iPad5,2"]) return iPad_Mini_3_Cellular; demo是这个,文档是ipad mini 4
        si1ence:@Minoz_min demo写错了,已修改!
      • Minoz_min:demo里面 if ([machineString isEqualToString:@"iPhone10,2"]) return Global_iPhone_8;这个写错了,应该是Global_iPhone_8_Plus
        si1ence:@Minoz_min 感谢感谢!已修改
      • F森:获取MAC地址可以上线?
        si1ence:@F森 之前是可以上线的,最近不清楚了
      • 月下独酌灬:楼主这些设备型号在哪查的啊:grin:
        si1ence:@si1ence 10,1 10,2 10,4 10,5 都出来了,那你说说看10,3 10,6 是哪个手机?
        si1ence:@月下独酌灬 这个都是有顺序和套路的,苹果这个型号又不是天马行空随便编出来的
        月下独酌灬:如果是Google的话怎么验证的啊,iPhone X都没出来呢
      • 燃烧的指尖滑落:你这个UIDevice 怎么还有中文呢?这只能操作系统是中文的吧?
        si1ence:@燃烧的指尖滑落 设置的什么,拿到的就是什么,国外可以设置中文,国内也可以设置英文
        燃烧的指尖滑落:@si1ence 什么意思?不太懂.如果国外使用这个方法得到的UIDevice也会有中文么?
        si1ence:@燃烧的指尖滑落 这里只涉及到 get 方法
      • 里奇001:楼主怎么知道iPhoneX的设备型号的?
        si1ence:@里奇001 Google knows everything:flushed:
      • 8a567a6ebd2e:灰常感谢及时的更新~但是这代iphone一下更新了6个型号,有啥说法?莫非只是按照内存容量?
        si1ence:@刘三太子 已更新文章
        8a567a6ebd2e:@si1ence 感谢辛劳的题主
        si1ence:@刘三太子 应该是国行还是美版,下午细化一下
      • J_Pisces:想问一下, 获取这些需要询问用户吗?:flushed:
        si1ence:@J_Pisces 只有权限类需要,其他设备信息不需要
      • WELCommand:很难想象除了Apple 官网居然可以看到这么权威的文章。看到这篇文章,花光了我所有的运气。:relaxed:
        si1ence:@WELCommand :sweat:
      • 錒伟:UDID现在还能用吗
      • 尼古拉斯骄傲:666 尤其这个判断设备型号的
      • FanChason:demo打开类文件不全,报红了。运行报错,找不到文件。楼主代码是不是提交的不对。
        si1ence:@CHASON_FAN Hi~Github已更新为可用项目
        FanChason:@si1ence 好的,坐等楼主更新。哈哈
        si1ence:@CHASON_FAN 嗯,前几天有一部分没弄完,最近略忙就没弄全,抱歉,本周末会更新
      • 呆呆滴木木菇凉:这个ip地址代码只能获取wifi情况下的
        si1ence:@Coffee_LaFa 是的
      • 进击的曹尼玛:真机跑跑就知道结果了。iOS9 、iOS10 看看看就知道结果,尼玛,这破事,我折腾了N久,很多都拿不到的,比如mac 地址变成了固定 uuid 什么序列号的都是拿不到的,采用uuid + keychain 重置一下手机,还不是全部都变了,现在的iOS没有唯一标识。
      • 054ce4d15908:怎么通过IP地址,获取对应设备的名称,楼主
      • e8a00e2ada25:device token crc32含义 是什么? 能贴一下代码么?
      • 百无一用是码农:不错不错 很全
      • falc0n:非常不错,感谢总结
      • wico77:请问用js能获得哪些信息?
        si1ence:@wico77 js只是一种h5和native交互的方式,只要native可以获取的,都可以传给js
      • ebay_Happy:谢谢大神,好评
      • 35444b096164:uuid是一种格式,不是一种id,你这里取到的值叫idfv更合适,另外MAC地址在真机上(>=iOS7.0)都是02:00:00:00:00:00
      • Callmewenxi:其实现在获取用户的UDID上架时会被苹果拒绝吗?IDFA现在有可能获取不到了,不知道怎么标识用户设备
        si1ence:@iOS小小白 10.3升级后这个会变,这个在我 邀请机制那个文章里讲过
      • 我不是掌柜:小弟写了一个信息采集的,https://github.com/ScottZg/ZGInfoCollection
        e8a00e2ada25:来砸场子的吧
        aef3d41f9e18: @kakarotto 能加下微信交流下吗?我微信273698351
        si1ence:@kakarotto :+1: :+1:
      • king锋:好全呀收藏:smile:
      • 心语风尚:为什么没有看到device token方法内容
      • 5c411a02800d:请问题主有没有判断用户手机是否国行的方法?不是混在一起的
        si1ence:@emilyZZ 上面代码不是有么?
      • 杂雾无尘:iPhone 7和7P该更新进去了
        si1ence:@杂雾无尘 ok!头像好评!
      • 091155f824ff:唯一识别码UUID这个有问题的,如果卸载了再重新看会发现不同了,可以试试。ios5以后苹果不允许访问UDID,只能通过别的方式做成类似UDID40位参数的值,UUID是替代品(32位),用开源的获取UUID当做设备识别码吧
      • Fatm:能获取手机号吗
      • 卜卜星:13年5月起苹果禁止应用使用UDID、MAC地址, 因为可以轻松的标识出“唯一用户”. 应用里使用UDID等会被下架的
      • 温暖的弦Y:楼主不容易啊
      • code_间特门:代码届之集大成者。 :grin:
      • Peak_One:这个留着绝壁有用
        si1ence:@Peak_One 那是必须的,哈哈
      • 柯浩然:很全面!!
      • 晓晓德:刚好需要这个!赞!
      • 早抖:看不懂
      • Ryan文濤:小伙子,可以! :smile:

      本文标题:史上最全的iOS各种设备信息获取总结(iPhone X 详细信息

      本文链接:https://www.haomeiwen.com/subject/jsafjttx.html