Continuing the discussion from Add to Home Screen on click :
What I’m trying to do:
I am trying to understand the code below so I can adapt it to show the button if the app has not been installed. If it has been installed then the button should be hidden. At the moment I cannot even get the sample below to work so I must be missing something.
Optional - use local storage to check if we’ve shown a prompt before
from anvil_extras.storage import local_storage
def get_visited():
return local_storage.get("seen_prompt")
def set_visited():
local_storage["seen_prompt"] = True
Add an event handler for the beforeinstallprompt
event
from anvil.js import window
install_event = None
def before_install(e):
global install_event
e.preventDefault() # stops the default prompt displaying
if get_visited():
# don't show the prompt if it's been show
return
install_event = e
get_open_form().show_install_btn()
window.addEventListener('beforeinstallprompt', before_install);
add some logic to the main form
class Form1(Form1Template):
def show_install_btn(self):
self.install_prompt_btn.visible = True
def install_prompt_btn_click(self, **event_args):
"""This method is called when the button is clicked"""
global install_event
if install_event is None:
# sanity check
self.install_prompt_btn.visible = False
return
install_event.prompt()
# install_event.userChoice is a javascript Promise and we wait for the result
# use anvil.js.await_promise() to do this
result = anvil.js.await_promise(install_event.userChoice)
if result.get("outcome") == "accepted":
alert("User accepted the A2HS prompt") # Add To Home Screen (A2HS)
else:
alert("User dismissed the A2HS prompt")
install_event = None
self.install_prompt_btn.visible = False
set_visited()
Here’s an example clone link:
Anvil | Login
What I’ve tried and what’s not working:
Installed the clone and ran it as is but the install button never shows.
Is there something In am missing to make this run as is (before I adapt it)?
Code Sample:
As above
Clone link:
As above
1 Like
If you just remove the if get_visited():
logic you’ll see the button visible more consistently.
That logic is supposed to prevent users seeing the install button more than once.
2 Likes