1

I am working on an app which needs to display some device information, Could some one help me to verify if that's legal to pull this information and maybe some code snippets would be very helpful.

Thanks in advance !

Device information:

  • Remain Battery Status
  • Is phone charging
  • Device OS version
  • Current App version
  • Available Memory
  • Available Storage
Grant Birchmeier
  • 15,933
  • 11
  • 58
  • 83
user3276557
  • 211
  • 1
  • 2
  • 5
  • 4
    This is not a good question, it lacks research effort. What have you done so far for solving this problem? – Volker Feb 06 '14 at 18:50

2 Answers2

14

I think all those information are legal to pull. and here are some code for each of them

  1. Remain battery:

    UIDevice *device = [UIDevice currentDevice];
    [device setBatteryMonitoringEnabled:YES];
    float remainBatteryLife = [myDevice batteryLevel];
    
  2. Phone charging status

    //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. OS version

    NSString *OSVersion = [[UIDevice currentDevice] systemVersion];
    
  4. App version

    NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
    
  5. Available Memory (For this question, i used some C codes instead of Objective-C, but that returns exactly like itunes would tell you, inspired by 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. Disk space (code source From Code Warrior on https://stackoverflow.com/a/8036586/3276557)

    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]);
    }  
    
Community
  • 1
  • 1
Xu Yin
  • 3,932
  • 1
  • 24
  • 46
1

You are looking for the UIDevice class. Some code sni

UIDevice *currentDevice = [UIDevice currentDevice];
NSString * batteryLevel = [currentDevice batteryLevel];
NSString *systemVersion = [currentDevice systemVersion];
Mika
  • 5,497
  • 5
  • 33
  • 77