Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine accurate iPhone battery level

Tags:

People also ask

At what percentage should I replace my iPhone battery?

At What Battery Health Percentage Should I Replace My iPhone Battery? Your iPhone should retain up to 80 percent of its original battery capacity after 500 complete charge cycles. Once the battery health percentage drops below 80 percent, the amount of charge your battery can hold starts to diminish.

Is battery capacity on iPhone accurate?

iPhone's battery health information is not accurate, testing finds.

How do I know if my battery is healthy?

Go to settings > Battery and device care > Diagnostics. You can now tap on battery status to check its health status. There are other features as well (Camera, speaker and more) of the phone that you can test to see if they are working fine or you should get them fixed.


I'm trying to get the battery level/state as follows:

- (void) viewWillAppear: (BOOL) animated{

    [self viewWillAppear:animated];

    // Battery state 
    [self batteryStatus];
    [[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)
    {
        [textViewStatus setText:[batteryStatus objectAtIndex:0]];
        NSLog(@"%@", [batteryStatus objectAtIndex:0]);
    }
    else
    {
        NSString *msg = [NSString stringWithFormat:@"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100,[batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ];
        [textViewStatus setText:msg];
        NSLog(@"%@", msg);
    }
}

However, I am not getting changes in the battery level, and the values are not exact.

- (void) viewWillAppear:(BOOL)animated {
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    textViewStatus.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel];
    [device addObserver:self forKeyPath:@"batteryLevel" options:0x0 context:nil];
    [super viewWillAppear:animated];
}

- (void) viewDidDisappear:(BOOL)animated {
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = NO;
    [device removeObserver:self forKeyPath:@"batteryLevel"];
    [super viewDidDisappear:animated];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    UIDevice *device = [UIDevice currentDevice];
    if ([object isEqual:device] && [keyPath isEqual:@"batteryLevel"]) {
        NSLog(@"Battery level :%f",device.batteryLevel);
        textViewStatus.text = [NSString stringWithFormat:@"%.2f", device.batteryLevel];
    }
}

Any ideas?