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 @@ ...@@ -55,6 +55,7 @@
eventReceiver:(FlutterEventReceiver)eventReceiver { eventReceiver:(FlutterEventReceiver)eventReceiver {
_eventReceiver = eventReceiver; _eventReceiver = eventReceiver;
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[self sendBatteryStateEvent];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self addObserver:self
selector:@selector(onBatteryStateDidChange:) selector:@selector(onBatteryStateDidChange:)
...@@ -64,6 +65,10 @@ ...@@ -64,6 +65,10 @@
} }
- (void)onBatteryStateDidChange:(NSNotification*)notification { - (void)onBatteryStateDidChange:(NSNotification*)notification {
[self sendBatteryStateEvent];
}
- (void)sendBatteryStateEvent {
if (!_eventReceiver) return; if (!_eventReceiver) return;
UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState]; UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState];
switch (state) { switch (state) {
...@@ -76,8 +81,8 @@ ...@@ -76,8 +81,8 @@
break; break;
default: default:
_eventReceiver([FlutterError errorWithCode:@"UNAVAILABLE" _eventReceiver([FlutterError errorWithCode:@"UNAVAILABLE"
message:@"Charging status unavailable" message:@"Charging status unavailable"
details:nil]); details:nil]);
break; break;
} }
} }
......
...@@ -47,6 +47,7 @@ import Flutter ...@@ -47,6 +47,7 @@ import Flutter
eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? { eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? {
self.eventReceiver = eventReceiver; self.eventReceiver = eventReceiver;
UIDevice.current.isBatteryMonitoringEnabled = true; UIDevice.current.isBatteryMonitoringEnabled = true;
self.sendBatteryStateEvent();
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
self, self,
selector: #selector(onBatteryStateDidChange), selector: #selector(onBatteryStateDidChange),
...@@ -56,6 +57,10 @@ import Flutter ...@@ -56,6 +57,10 @@ import Flutter
} }
@objc private func onBatteryStateDidChange(notification: NSNotification) { @objc private func onBatteryStateDidChange(notification: NSNotification) {
self.sendBatteryStateEvent();
}
private func sendBatteryStateEvent() {
if (eventReceiver == nil) { if (eventReceiver == nil) {
return; 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