in iOs isn't working properly
BerthaBrenes opened this issue · comments
Bug Report
Problem
The plugging isn't working on iOS devices, the code haven't been update after 7 years
What is expected to happen?
Its suppose to show the battery status
What does actually happen?
Doesn't show anything, no even a simple console log in the function
Information
node v12.16.1
npm 6.13.4
ionic CLI 6.0.2
Command or Code
here my simple code
this.batteryStatus.onChange().subscribe(status => {
console.log("Estado Bateria");
console.log(status.level, status.isPlugged);
this.batteryText = status.level;
this.cdr.detectChanges();
});
Environment, Platform, Device
- [X ] I searched for existing GitHub issues
- [ X] I updated all Cordova tooling to most recent version
- [ X] I included all the necessary information above
Can you confirm that you see the same behaviour on a sample pure-cordova app (using no frameworks like ionic)? This will help us confirm that the issue is indeed with this plugin, and not something between else in between. Thanks.
https://github.com/apache/cordova-contribute/blob/master/create-reproduction.md
I can partially confirm it. iOS behaves like this: when you subscribe to the event, initially no update is triggered. only when the battery status changes (like battery drains or device is plugged in/out) the battery status update is fired.
this is one pure-cordova app, ios 13. on device and simulator.
looks like #77 adresses this problem.
Here is a trick on how to immediately obtain current battery status on IOS:
window.addEventListener("batterystatus", emptyFunc);
window.removeEventListener("batterystatus", emptyFunc); // <-- this line force Plugin to obtain actual status
// Now subscribe again and immediately receive the actual status
window.addEventListener("batterystatus", onBatteryStateChange);
Why unsubscribe forces to read the status ?
When we do unsubscribe from batterystatus
event and no more listeners available then plugin calls stop
command that actually does:
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self getBatteryStatus]];
@u01jmg3 @platov @esskar @BerthaBrenes @breautek Instead of maintaining this plugin only for me, I published a new maintained plugin called "community-cordova-plugin-battery-status" (https://github.com/EYALIN/community-cordova-plugin-battery-status) which is maintained and will keep being.
please feel free to use it, and write me any feedback.
can be installed by using "cordova plugin add community-cordova-plugin-battery-status"
all Android and IOS recent issues are fixed there.