The Insomnia plugin promises to keep devices awake with window.plugins.insomnia.keepAwake()
, until such time as window.plugins.insomnia.allowSleepAgain()
is called.
But for some apps it only makes sense for the device to stay awake while the app is active. If the user pauses the app and forgets they have left it going in the background, it would be nice to allow sleep rather than decimate their battery level.
Typically actions required on app pause are handled through the pause
event:
function onDeviceReady() {
document.addEventListener("pause", onPause, false);
}
function onPause() {
window.plugins.insomnia.allowSleepAgain();
}
But according to Cordova docs, the Pause handler on iOS can't call anything native - which would include the Insomnia plugin.
How to achieve the desired functionality?
Looking at the source of that plugin, on iOS it calls
setIdleTimerDisabled:true
on theUIApplication
instance.isIdleTimerDisabled
isIt doesn't disable the idle timer for the device as a whole, so if the user suspends your app then the idle timer will be enabled again.