richtr / NoSleep.js

Prevent display sleep and enable wake lock in any Android or iOS web browser.
MIT License
2.24k stars 379 forks source link

nosleep.enable() not working on bookmark saved as an application in iphone #161

Open tazo-taz opened 8 months ago

tazo-taz commented 8 months ago

Expected Behavior

The noSleep.enable() function should prevent the screen from sleeping when the web application is launched from a bookmark saved as an application on an iPhone.

Actual Behavior

The noSleep.enable() function does not prevent the screen from sleeping when the web application is launched from a bookmark.

Code


useEffect(() => {
    const noSleep = new NoSleep();
    const documentOnClick = async () => {
      try {
        const a = await noSleep.enable();
        console.debug("no sleep enabled", a);
      } catch (e) {
        console.debug("no sleep prevented");
      }
    };
    document.addEventListener("click", documentOnClick);
    return () => {
      document.removeEventListener("click", documentOnClick);
      noSleep.disable();
    };
  }, []);
av01d commented 6 months ago

This is a known bug on iOS: https://bugs.webkit.org/show_bug.cgi?id=254545

Finesse commented 5 months ago

The video method works both in PWA and Safari on iOS 17. But the library doesn't use it because navigator.wakeLock is available. So the library maintainer should either use both navigator.wakeLock and video, or provide an option for us to choose.

As user, you can copy the library code to your website and modify it so both methods are used.

tazo-taz commented 4 months ago

@Finesse thank you for response, I tried that by break if else if else and it will enter into both condition but still has same problem on PWA any more ideas to fix or did you manage you to fix that

Finesse commented 4 months ago

@tazo-taz No, I failed to make it work reliably. I chose to use my app in Safari, because wake lock works more reliably there. Though it requires user action to activate, so I made an infinite loop waiting for any user action, then waiting for a lock release, then waiting for user action and so on.