iOS扩展-获取设备、应用相关信息

1. 获取设备的相关信息

基本信息
    [[UIDevice currentDevice] systemName];       // 系统名  e.g. @"iOS"
    [[UIDevice currentDevice] systemVersion];    // 版本号  e.g. @"9.3"
    [[UIDevice currentDevice] model];            // 设备类型 e.g. @"iPhone", @"iPod touch"
    [[UIDevice currentDevice] name];             // 设备名称 eg. @"Jaesun"
    [[UIDevice currentDevice] localizedModel];   // 本地模式
    [[UIDevice currentDevice] batteryLevel];     // 设备剩余电量(满状态为 1.0)  eg. 0.55
设备型号

新增型号与名称对应表在官网:https://www.theiphonewiki.com/wiki/Models 可查

// 需要引入头文件
#import "sys/utsname.h"

/**
 *  获取设备硬件类型并转化为熟知的硬件类型名称
 *  参照 :https://www.theiphonewiki.com/wiki/Models
 *  @return 硬件类型俗称
 */
- (NSString*)deviceModelDetail {
    
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *deviceModelDetail = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
   
    if ([deviceModelDetail isEqualToString:@"iPhone8,4"])   return @"iPhone SE";
    if ([deviceModelDetail isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceModelDetail isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    
    if ([deviceModelDetail isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceModelDetail isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    
    if ([deviceModelDetail isEqualToString:@"iPhone6,1"])    return @"iPhone 5s";
    if ([deviceModelDetail isEqualToString:@"iPhone6,2"])    return @"iPhone 5s";
    
    if ([deviceModelDetail isEqualToString:@"iPhone5,4"])    return @"iPhone 5c";
    if ([deviceModelDetail isEqualToString:@"iPhone5,3"])    return @"iPhone 5c";
    if ([deviceModelDetail isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceModelDetail isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    
    if ([deviceModelDetail isEqualToString:@"iPhone4,1"])    return @"iPhone 4S ";
    
    if ([deviceModelDetail isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceModelDetail isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([deviceModelDetail isEqualToString:@"iPhone3,3"])    return @"iPhone 4";

    if ([deviceModelDetail isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceModelDetail isEqualToString:@"x86_64"])       return @"Simulator";

    return deviceModelDetail;
}

系统语言信息
// 获取设备系统当前语言
- (NSString *)preferredLanguage
{
    NSArray *languages = [NSLocale preferredLanguages];
    NSString * preferredLanguage = [languages objectAtIndex:0];
    return preferredLanguage;
}
内存,磁盘空间相关信息
//需要 引入头文件
#include 
#include 
#include 

(1)-获取内存(运行内存)总容量,和内存可用容量

/**
 *  获取内存总容量(运行内存)
 *
 *  @return
 */
-(long long)totalMemoryLength
{
    return [NSProcessInfo processInfo].physicalMemory;
}

/**
 *  获取可用内存
 *
 *  @return
 */
-(long long)freeMemorylength
{
    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));
}

(2)-获取磁盘(储空间内存,我们应用存储数据文件的地方)总容量,和磁盘可用容量

/**
 *  获取总磁盘容量(存储内存)
 *
 *  @return
 */
-(long long)totalDiskSize
{
    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;
}

/**
 *  获取可用磁盘容量
 *
 *  @return
 */
-(long long)freeDiskLength
{
    struct statfs buf;
    unsigned long long freeLength = -1;
    if (statfs("/var", &buf) >= 0)
    {
        freeLength = (unsigned long long)(buf.f_bsize * buf.f_bavail);
    }
    return freeLength;
}
获取设备IP
引入头文件
#import 
#import 
#import 
#import 
#import 

获取设备当前网络环境下的IP地址


/**
 * 获取设备IP
 */
- (NSString* )deceiveIP {
    
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) return nil;
    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;
}

2. 获取应用的相关信息

基本信息
  //app应用相关信息的获取
    NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];
    // 当前应用名称
    [dicInfo objectForKey:@"CFBundleDisplayName"];
    // 当前应用软件版本  比如:1.0.1
    [dicInfo objectForKey:@"CFBundleShortVersionString"];
    // 当前应用版本号码   int类型
    [dicInfo objectForKey:@"CFBundleVersion"];
获取Appstore中应用相关信息 -- 如 : 版本号等信息

相关数据从 https://itunes.apple.com/lookup?id=(应用的Apple id)获取, 应用的Apple id 在iTunes Connect--APP信息中查看

-  (void)versionInAppStore {
   
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/lookup?id=533314804"];
    NSURLRequest  *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
           // appStore 中应用相关信息
            NSString *JSONStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"JSONStr IS %@",JSONStr);
            NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        // 可以获取已经上架的而应用版本号
         NSString *versionInStore = [[[JSONDic objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"];
        }
    }];
    [dataTask resume];
}

你可能感兴趣的:(iOS扩展-获取设备、应用相关信息)