公式ドキュメントにも、クラスのクラスダンプされたプライベートヘッダーにも、このためのメソッドは見つかりませんでしたUIDevice
。
だから私たちは何かを考え出さなければなりません。私が現時点で考えている最善の「解決策」は、ダウンロード時間を見積もるときに採用したアプローチに似ています。ダウンロード/充電の平均速度を計算し、残りの量(データまたは充電)をその速度で除算します。
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float prevBatteryLev = [UIDevice currentDevice].batteryLevel;
NSDate *startDate = [NSDate date];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(batteryCharged)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil
];
- (void)batteryCharged
{
float currBatteryLev = [UIDevice currentDevice].batteryLevel;
// calculate speed of chargement
float avgChgSpeed = (prevBatteryLev - currBatteryLev) / [startDate timeIntervalSinceNow];
// get how much the battery needs to be charged yet
float remBatteryLev = 1.0 - currBatteryLev;
// divide the two to obtain the remaining charge time
NSTimeInterval remSeconds = remBatteryLev / avgChgSpeed;
// convert/format `remSeconds' as appropriate
}