platov commented on issue #78:
URL: 
https://github.com/apache/cordova-plugin-battery-status/issues/78#issuecomment-1195422269

   Here is a trick on how to obtain current battery status on IOS without need 
of waiting of change event:
   
   ```javascript:
   window.addEventListener("batterystatus", emptyFunc);
   window.removeEventListener("batterystatus", emptyFunc); // <-- this line 
force Plugin to obtain actual status
   
   // Now read the actual status
   const level = navigator.battery._level;
   const isPlugged = navigator.battery._isPlugged;
   
   // listen on further status updates.
   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]];
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to