check if user has already installed PWA to homescreen on Chrome?

前端 未结 5 1382
孤城傲影
孤城傲影 2020-12-24 02:04

I\'m trying to create an \"Add To Home Screen\" button on my progressive web app, as described in Chrome\'s documentation.

I\'m generally following the prescribed p

5条回答
  •  天涯浪人
    2020-12-24 02:33

    I don't see how this is the correct answer, because this is basically a check if user uses the App already, but the behavior we wan't is "When the user is on the web and tries to install the app again to tell him that he already has the app in his device". Upon me this is not an answer that solves this.

    What we can do is: 1. When the user clicks install but has the application on his device In this case the beforeinstallprompt event WON'T BE fired so this event will return null. We store the result in global variable and when the result is null we show this to user that he already has the app installed. 2. When the user clicks install but doesn't have the application on his device In this case the beforeinstallprompt event WILL be fired so this event will return access to show the prompt. We can store the result in global variable and if it is not NULL (which won't be) because beforeinstallprompt will be fired if the user don't have the app on his device we show the prompt() to the user.

    I doubt if mine solution is good too but I think that the Question and the correct answer don't have nothing in common

    window.addEventListener("beforeinstallprompt", event => {
      window.deferedPrompt = event;
    });
    
    handleButtonClick = () => {
     const promptEvent = window.deferedPrompt;
     if(!promptEvent){
     // DO SOMETHING
     }
    //Show the add to home screen prompt
    promptEvent.prompt()
    
    promptEvent.userChoice.then((result: any) => {
          // Reset the deferred prompt variable, since
          // prompt() can only be called once.
          window.deferedPrompt = null;.
        });
    }
    
    
    
    
    

提交回复
热议问题