获取操作系统版本,电池,存储,内存等系统信息

use*_*557 1 ios

我正在开发一个需要显示一些设备信息的应用程序,有人可以帮我验证是否合法提取这些信息,可能一些代码片段会非常有用.

提前致谢 !

设备信息:

  • 保持电池状态
  • 电话充电
  • 设备OS版本
  • 当前应用版本
  • 有效内存
  • 可用存储

Xu *_*Yin 12

我认为所有这些信息都是合法的.这里有一些代码

  1. 保持电量:

    UIDevice *device = [UIDevice currentDevice];
    [device setBatteryMonitoringEnabled:YES];
    float remainBatteryLife = [myDevice batteryLevel];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 手机充电状态

    //same device object as the previous one
    UIDevice *device = [UIDevice currentDevice];
    [device setBatteryMonitoringEnabled:YES];
    int i=[myDevice batteryState];
    
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
            //Unplugged
            break;
        case UIDeviceBatteryStateCharging:
            //Charging
            break;
        case UIDeviceBatteryStateFull:
            //full        
            break;
        default:
            break;
        }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 操作系统版本

    NSString *OSVersion = [[UIDevice currentDevice] systemVersion];
    
    Run Code Online (Sandbox Code Playgroud)
  4. 应用版本

    NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    Run Code Online (Sandbox Code Playgroud)
  5. 可用内存(对于这个问题,我使用了一些C代码而不是Objective-C,但它的回归与itunes告诉你的完全相同,受https://sites.google.com/site/iphonedevnote/Home/get-available-的启发记忆)

    //import some C libraries first
    #import <mach/mach.h>
    #import <mach/mach_host.h>
    
    //then put these code in whichever method it needs to be
    mach_port_t host_port;
    mach_msg_type_number_t host_size;
    vm_size_t pagesize;
    
    host_port = mach_host_self();
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    host_page_size(host_port, &pagesize);
    
    vm_statistics_data_t vm_stat;
    
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
    NSLog(@"Failed to fetch vm statistics");
    
    /* Stats in bytes */
    natural_t mem_used = (vm_stat.active_count +
                      vm_stat.inactive_count +
                      vm_stat.wire_count) * pagesize;
    natural_t mem_free = vm_stat.free_count * pagesize;
    natural_t mem_total = mem_used + mem_free;
    natural_t memoryFactor = 1024;
    NSLog(@"used: %u MB free: %u MB total: %u MB", (mem_used / memoryFactor) / memoryFactor, (mem_free / memoryFactor) /memoryFactor, (mem_total /memoryFactor) /memoryFactor);
    
    Run Code Online (Sandbox Code Playgroud)
  6. 磁盘空间(来自Code Warrior的代码源,位于/sf/answers/562561051/)

    uint64_t totalSpace = 0;
    uint64_t totalFreeSpace = 0;
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    
    if (dictionary) {
        NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
        totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
        totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
        NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
    } else {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
    }  
    
    Run Code Online (Sandbox Code Playgroud)

  • @ user2509601谢谢.希望这会被接受.大声笑. (3认同)