简体   繁体   English

使用按钮将移动Web应用添加到主屏幕

[英]Adding mobile web app to home screen using button

I'm trying to add app to home screen via button located in "settings" page of my HTML5 mobile web app (Notice: It is working if I'm trying to add it via Chrome menu). 我正在尝试通过HTML5移动网络应用程序“设置”页面上的按钮将应用程序添加到主屏幕(注意:如果我想通过Chrome菜单添加应用程序,则可以使用)。

I've set up all of these steps: 我已经完成所有这些步骤:

  • The PWA must not already be installed 必须尚未安装PWA
  • Web app must include a web app manifest. Web应用程序必须包含Web应用程序清单。
  • Web app must be served over a secure HTTPS connection. Web应用程序必须通过安全的HTTPS连接进行服务。
  • Web app has registered a service worker with a fetch event handler. Web应用程序已使用获取事件处理程序注册了服务工作者。

Currently I'm debugging it via Chrome Dev tools and listening to beforeinstallprompt with that code from official docs. 目前,我正在通过Chrome Dev工具进行调试,并使用官方文档中的代码收听beforeinstallprompt

<script>
let deferredPrompt;

window.addEventListener('beforeinstallprompt', function(event) {
  // Prevent Chrome 67 and earlier from automatically showing the prompt
  e.preventDefault();
  // Stash the event so it can be triggered later.
  deferredPrompt = e;
});

// Installation must be done by a user gesture! Here, the button click
btnAdd.addEventListener('click', (e) => {
  // hide our user interface that shows our A2HS button
  btnAdd.style.display = 'none';
  // Show the prompt
  deferredPrompt.prompt();
  // Wait for the user to respond to the prompt
  deferredPrompt.userChoice
    .then((choiceResult) => {
      if (choiceResult.outcome === 'accepted') {
        console.log('User accepted the A2HS prompt');
      } else {
        console.log('User dismissed the A2HS prompt');
      }
      deferredPrompt = null;
    });
});

</script>

How to catch event on /home page, and pass it to /settings page so I could fire the event via (onClick) event? 如何在/home页面上捕获事件,并将其传递到/settings页面,以便我可以通过(onClick)事件触发该事件?

Currently I'm using: 目前,我正在使用:
Angular CLI: 6.1.4 角CLI:6.1.4
Node: 8.11.4 节点:8.11.4
OS: win32 x64 操作系统:win32 x64

将其保存到全局( window )对象(或具有全局访问权限的其他位置)以及事件处理程序调用window.deferredPrompt.prompt() /settings页面上吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果添加到主屏幕,则JavaScript无法与支持apple-mobile-web-app的应用一起使用 - JavaScript doesn't work with apple-mobile-web-app-capable if add to Home Screen window.open()在添加到主屏幕的移动Safari Web应用程序中不起作用 - window.open() doesn't work in mobile Safari web-app added to home screen 谷歌网络应用程序添加到主屏幕 - google web app add to home screen 如何将Web应用添加到用户主屏幕? - How to add web app to user home screen? jQuery Mobile,在主屏幕上将新页面保留在应用程序中 - JQuery Mobile, Keep new page in app on home screen 移动响应式Web应用程序,通过下一步按钮在屏幕上分别显示两个div - Mobile responsive web app that displays two divs individually on screen with next button 添加主屏幕图标以创建 React 应用程序时遇到问题 - Having trouble with adding home screen icons to create react app 添加缩放以在移动应用程序上捏合 web map 应用程序 - Adding zoom to pinch on a mobile web map app iOS Web应用程序 - 仅在添加到主屏幕时显示内容? - iOS Web app - show content only if added to home screen? 有什么方法可以检测用户是否已将Web应用程序添加到其主屏幕? - Is there any way to detect if a user has added a web app to their home screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM