Commit 752d6096 authored by Sarah Zakarias's avatar Sarah Zakarias Committed by GitHub

Query battery state on startup in iOS Platform Channel examples (#9437)

parent 3dfe225c
......@@ -55,6 +55,7 @@
eventReceiver:(FlutterEventReceiver)eventReceiver {
_eventReceiver = eventReceiver;
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[self sendBatteryStateEvent];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onBatteryStateDidChange:)
......@@ -64,6 +65,10 @@
}
- (void)onBatteryStateDidChange:(NSNotification*)notification {
[self sendBatteryStateEvent];
}
- (void)sendBatteryStateEvent {
if (!_eventReceiver) return;
UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState];
switch (state) {
......@@ -76,8 +81,8 @@
break;
default:
_eventReceiver([FlutterError errorWithCode:@"UNAVAILABLE"
message:@"Charging status unavailable"
details:nil]);
message:@"Charging status unavailable"
details:nil]);
break;
}
}
......
......@@ -47,6 +47,7 @@ import Flutter
eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? {
self.eventReceiver = eventReceiver;
UIDevice.current.isBatteryMonitoringEnabled = true;
self.sendBatteryStateEvent();
NotificationCenter.default.addObserver(
self,
selector: #selector(onBatteryStateDidChange),
......@@ -56,6 +57,10 @@ import Flutter
}
@objc private func onBatteryStateDidChange(notification: NSNotification) {
self.sendBatteryStateEvent();
}
private func sendBatteryStateEvent() {
if (eventReceiver == nil) {
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment