I recently updated to macOS Sonoma 14.4 and now UIDevice.current.batteryLevel
is always 0
.
Code to reproduce:
import SwiftUI
struct ContentView: View {
@State
private var monitoringEnabled = UIDevice.current.isBatteryMonitoringEnabled;
@State
private var batteryLevel = UIDevice.current.batteryLevel;
var body: some View {
VStack {
Text("Battery Monitoring Enabled: " + String(monitoringEnabled))
Text("Battery Level: " + String(batteryLevel))
Button("Toggle Monitoring") {
monitoringEnabled = !monitoringEnabled;
UIDevice.current.isBatteryMonitoringEnabled = monitoringEnabled;
batteryLevel = UIDevice.current.batteryLevel;
}
}
.padding()
}
}
Run the above on a macOS 14.4 target, click "Toggle Monitoring", and you'll see battery level is reported as 0
:
I also see the following error in my app logs when running on macOS 14.4:
Error retrieving battery status: result=-536870207 percent=0 hasExternalConnected=1 isCharging=0 isFullyCharged=0
This code displays the expected battery level when running on an actual iOS device: