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

This commit is contained in:
Sarah Zakarias
2017-04-18 14:59:33 +02:00
committed by GitHub
parent 3dfe225cb9
commit 752d6096a6
2 changed files with 12 additions and 2 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}