iOS获取设备 电池-内存-磁盘信息

import "UIDevice+Category.h"

import

@implementation UIDevice (Category)

  • (CGFloat)deviceGetBatteryQuantity {

    [UIDevice currentDevice].batteryMonitoringEnabled = YES;

    return ([[UIDevice currentDevice] batteryLevel]) * 100;
    }

  • (long long)deviceGetTotalMemorySize {

    return [NSProcessInfo processInfo].physicalMemory;
    }

  • (long long)deviceGetTotalDiskSize {
    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;
    }

  • (long long)deviceGetAvailableDiskSize {
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0)
    {
    freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
    }
    return freeSpace;
    }

/******************************/

import "ViewController.h"

import "UIDevice+Category.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat battery = [UIDevice deviceGetBatteryQuantity];

    long long memorySize = [UIDevice deviceGetTotalMemorySize];

    long long diskSize = [UIDevice deviceGetTotalDiskSize];

    long long availableDiskSize = [UIDevice deviceGetAvailableDiskSize];

    NSLog(@"\n battery:%f \n memorySize:%lld \n diskSize:%lld \n availableDiskSize:%lld ",battery,memorySize,diskSize,availableDiskSize);
    /**
    battery:100.000000
    memorySize:1048576000
    diskSize:12075704320
    availableDiskSize:3585474560
    */
    }

Demo https://github.com/550872569/UIDeviceCategory.git

你可能感兴趣的:(iOS获取设备 电池-内存-磁盘信息)