18

Regarding iPad events, how to determine if/when the iPad is changing from an awake state to a standby state?

What I would like to do is put my Mobile-Safari web app in a locked state whenever the iPad becomes inactive/standby and ask for a PIN when it becomes awake again.

Cœur
  • 37,241
  • 25
  • 195
  • 267
rcarrier
  • 193
  • 1
  • 1
  • 7

3 Answers3

34

I agree that there really ought to be some signal you can hook on to to know when an application goes to sleep and when it wakes up, but you should be able to figure out when Safari wakes up indirectly.

When the webview goes into the background, Safari puts everything in it to sleep. It pauses any video, defers network request, stops updating the UI and pauses all setInterval/setTimeout operations. JS itself will never be aware (as far as I can tell) how these things happened, but it can tell that it has happened. The simplest way to use this is to build a regularly invoked method and check to see if it's been an unexpectedly LONG time since the last update. If you expect something to update every 10 seconds and it's been five minutes, you can be fairly sure that the device has woken up. Here's a quick example I thought up:

    var intTime = new Date().getTime();
    var getTime = function() {
        var intNow = new Date().getTime();
        if (intNow - intTime > 1000) {
            console.log("I JUST WOKE UP")
        }
        intTime = intNow;
        setTimeout(getTime,500);
    };
    getTime();

This will detect when the user has returned from another tab, has dismissed the developer console or brought back Safari from the background. I've set the interval at half a second; you could set it at anything you need, though I think very low values will have concurrency issues and probably will burn the battery down on the device unnecessarily.

Andrew
  • 14,204
  • 15
  • 60
  • 104
  • Thank you Andrew. I already have "idle" timers in my web app which is a webmail with an IM client among other interesting features. I had not thought about using one such timer to detect if the iPad has been inactive for a period of time. I guess that will have to do for now. Cheers! – rcarrier Feb 09 '11 at 15:28
  • I think you can decrease the interval to check to 1 or two seconds because most of actions take at least 1 or 2 seconds to do if you are not paranoid ;-). – Codebeat Nov 11 '12 at 02:04
  • @Rima Depends on the application, but very generally, yes. If you have a CPU intensive application, this sort of polling will have nearly no impact (the logic required is minimal and the native performance hit of getting a timestamp is negligible.) But for something like a web form, where the user may leave the page untouched for long periods, I would expect polling like this to very minutely, but measurably affect battery life. Also, use `Date.now()` (this wasn't available in 2011) since that will save an unnecessary stack frame. So your timeout should be as long as possible. – Andrew Oct 19 '18 at 10:52
  • 1
    Setting the tab in background always triggered the `> 1000` condition. Setting the value to `2000` only activated when it came back from stand-by. Actually, this value can be as high as 10 sec, because no computer can go in and out of stand-by in less than 10 sec. – Binar Web Apr 20 '21 at 07:56
1

The Mobile Safari does not have access to this level of hardware state. The most I think it has is to the accelerator.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Thanks Daniel, I just found this link also related to this http://stackoverflow.com/questions/4401764/what-event-fires-when-a-webkit-webapp-is-terminated – rcarrier Feb 09 '11 at 02:34
  • I just can't imagine that it would be so hard to send apps (including Mobile Safari) a "imminent-termination-warning" signal that you could listen to and plan accordingly. I mean iOS probably has that sort of hardware state info itself..."simply" pushing it to the application layer *should* be doable? – rcarrier Feb 09 '11 at 02:48
  • @rcarrier What would you do with such a signal? If you execute code after receiving the imminent termination, you would delay or, worse, prevent sleep. Imagine a "bad" web application preventing your mobile device to stand-by and depleting your battery. If you don't execute code, the signal itself is useless. – janesconference May 29 '13 at 11:20
  • You're absolutely right. I didn't think much about it at the time. That makes perfect sense. Thanks for your insight. – rcarrier May 29 '13 at 18:29
  • 1
    but they could provide a waking up function, which would serve the same purpose, and not block termination. – Todd Horst Mar 03 '14 at 20:08
0

One possible solution that worked for me is to use

$(window).on('blur', function(){});
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Dom
  • 1