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

时间:2014-02-06 18:47:38

标签: ios

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

提前致谢!

设备信息:

  • 保持电池状态
  • 手机充电
  • 设备操作系统版本
  • 当前应用版本
  • 可用内存
  • 可用存储空间

2 个答案:

答案 0 :(得分:12)

我认为所有这些信息都是合法的。以下是每个代码的一些代码

  1. 留出电池:

    UIDevice *device = [UIDevice currentDevice];
    [device setBatteryMonitoringEnabled:YES];
    float remainBatteryLife = [myDevice batteryLevel];
    
  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;
        }
    
  3. 操作系统版本

    NSString *OSVersion = [[UIDevice currentDevice] systemVersion];
    
  4. 应用版

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

    //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);
    
  6. 磁盘空间(来自https://stackoverflow.com/a/8036586/3276557上的Code Warrior的代码源)

    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]);
    }  
    

答案 1 :(得分:1)

您正在寻找UIDevice类。一些代码sni

UIDevice *currentDevice = [UIDevice currentDevice];
NSString * batteryLevel = [currentDevice batteryLevel];
NSString *systemVersion = [currentDevice systemVersion];