电池状态
ios的UIDevice提供了获取电池状态的相关属性。
batteryState 获取设备电池充电状态
batteryLevel 范围在0.00~1.00之间
如果执行某些操作需要判断电池电量是否满足要求可通过以下代码
-(BOOL)shouldProceedWithMinLevel:(NSUInteger)minLevel
{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
UIDeviceBatteryState state = device.batteryState;
if(state == UIDeviceBatteryStateCharging ||
state == UIDeviceBatteryStateFull) { //充电状态电池未满货、插电已满
return YES;
}
//获取电池电量百分比
NSUInteger batteryLevel = (NSUInteger) (device.batteryLevel * 100);
//判断是否满足最低要求电量
if(batteryLevel >= minLevel) {
return YES;
}
return NO;
}
CPU的使用率
获取cpu使用率需要使用到c库mach
#import <mach/mach.h>
-(float)appCPUUsage{
kern_return_t kr;
task_info_data_t info;
mach_msg_type_number_t infoCount=TASK