ios UIDeviceBattery【电池的状态处理:电池状态获取及监测、电池电量获取及监测、低电量模式切换监测】

前言

  • 需求:电量低于 <= 50 %进行报警
  • name:NSProcessInfoPowerStateDidChangeNotification

  • UIDeviceBatteryLevelDidChangeNotification

        [[NSNotificationCenter defaultCenter]
         addObserverForName:UIDeviceBatteryLevelDidChangeNotification
         object:nil queue:[NSOperationQueue mainQueue]
         usingBlock:^(NSNotification *notification) {
             // Level has changed
             [UIDevice currentDevice].batteryMonitoringEnabled = YES;
             NSLog(@"Battery Level Change");
             NSLog(@"电池电量:%.2f", [UIDevice currentDevice].batteryLevel);
         }];
    
    
  • UIDeviceBatteryStateDidChangeNotification

一、电池状态获取及监测

  • code

    #pragma mark - 电池状态获取及监控
    
    -(void)checkAndMonitorBatteryState{
    
    
    
    UIDevice * device = [UIDevice currentDevice];
    
    //是否允许监测电池
    
    //要想获取电池状态和监控电池状态 必须允许
    
    device.ba

你可能感兴趣的:(iOS开发基础)