Текущее состояние батареи всегда возвращается Зарядка в ios

Я получаю состояние батареи как UIDeviceBatteryStateCharging, хотя батарея моего iPhone заряжена на 100%.

Это должно дать мне состояние как UIDeviceBatteryStateFull.

Мой код выглядит следующим образом.

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);

person Rakesh    schedule 04.09.2014    source источник


Ответы (2)


это может помочь вам попробовать это.....

- (void)viewDidLoad
{
// Enable monitoring of battery status
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    // Print current status
    [self batteryStatus];

    // Request to be notified when battery charge or state changes
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];

}
 - (void)batteryStatus
{
 NSArray *batteryStatus = [NSArray arrayWithObjects: 
    @"Battery status is unknown.", 
    @"Battery is in use (discharging).", 
    @"Battery is charging.", 
    @"Battery is fully charged.", nil];

    if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown)
  {
        NSLog(@"%@", [batteryStatus objectAtIndex:0]);
  }
    else
  {     
    NSString *msg = [NSString stringWithFormat:
                                        @"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100,
                      [batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ];
        NSLog(@"%@", msg);
    }
}

а твой ответ...

вы проверяете другое значение ключа перечисления

typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // on battery, discharging
    UIDeviceBatteryStateCharging,    // plugged in, less than 100%
    UIDeviceBatteryStateFull,        // plugged in, at 100%
};              // available in iPhone 3.0

измените свой код, как это, он будет работать

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }

        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);

я надеюсь, что это поможет вам

person karthikeyan    schedule 04.09.2014

приложение ADC ведет себя так же... никогда не показывает FULL.

https://developer.apple.com/reference/uikit/uidevicebatteryleveldidchangenotification

так что Ларме кажется прав.

person ingconti    schedule 08.01.2017