如何修复我的桌面网络渐进式应用程序

时间:2018-10-22 16:37:29

标签: progressive-web-apps

我编写了以下简单代码,以使用Chrome 70创建和测试用于桌面的渐进式Web应用程序:

manifest.json

{
    "name": "App name",
    "short_name": "Short name",
    "scope": "/",
    "start_url": "/index.html",
    "display": "standalone",
    "background_color": "#2196F3",
    "theme_color": "#2196F3",
    "icons": [{
        "src": "images/icons/icon-128x128.png",
          "sizes": "128x128",
          "type": "image/png"
        }, {
          "src": "images/icons/icon-144x144.png",
          "sizes": "144x144",
          "type": "image/png"
        }, {
          "src": "images/icons/icon-152x152.png",
          "sizes": "152x152",
          "type": "image/png"
        }, {
          "src": "images/icons/icon-192x192.png",
          "sizes": "192x192",
          "type": "image/png"
        }, {
          "src": "images/icons/icon-256x256.png",
          "sizes": "256x256",
          "type": "image/png"
        }]
}

main.js

if (window.matchMedia('(display-mode: standalone)').matches) {
    console.log('display-mode is standalone');
  }

  // For Safari
  if (window.navigator.standalone === true) {
    console.log('display-mode is standalone');
  }



  let deferredPrompt;

window.addEventListener('beforeinstallprompt', (e) => {
  // Prevent Chrome 67 and earlier from automatically showing the prompt
  e.preventDefault();
  // Stash the event so it can be triggered later.
  deferredPrompt = e;
   // Update UI notify the user they can add to home screen
  // btnAdd.style.display = 'block';
});


  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;
      });
  });

    // If app is instaled
    window.addEventListener('appinstalled', (evt) => {
        app.logEvent('a2hs', 'installed');
      });

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="canonical" href="https://weather-pwa-sample.firebaseapp.com/final/">

    <title>Page Title</title>
    <!-- manifest  -->
    <link rel="manifest" href="/manifest.json">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>

    <!-- Add to home screen for Safari on iOS -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Weather PWA">
<link rel="apple-touch-icon" href="images/icons/icon-152x152.png">
<meta name="msapplication-TileImage" content="images/icons/icon-144x144.png">
<meta name="msapplication-TileColor" content="#2F3BA2">

</head>
<body>
    Hello World!
</body>
</html>

我以Chrome Server app的身份运行它,但没有要求我在主屏幕或其他任何地方进行安装。

我认为我的main.js很想念,那里有错误,可以指导我吗!

0 个答案:

没有答案