Reputation: 475
We have a Progressive Web App that prompts the user with the "Add to home screen" banner.
Adding to the homescreen works great, but after the user launches the page from the Home Screen it will sometimes still prompt them to install the app again. I'm posting here because all the resources I have found don't talk about this issue or how to solve it.
TL;DR Launching the app from the home screen still asks them to install the app with the "Add to home screen" prompt.
Upvotes: 2
Views: 792
Reputation: 1775
As suggested by @Mr.Rebot, I developed a little piece of code to solve the problem.
This is the result code:
window.addEventListener("beforeinstallprompt", (ev) => {
if (isStandalone()) {
// PWA already installed.
event.preventDefault();
return false;
} else {
// PWA not installed.
}
});
function isStandalone() {
// Check if device supports service workers
if (!('serviceWorker' in window.navigator)) return false;
// Check for Android
if (window.matchMedia('(display-mode: standalone)').matches) return true;
// Check for iOS
if (window.navigator["standalone"] == true) return true;
return false;
}
Upvotes: 1