如何添加更丰富的安装界面

应用商店为开发者提供了一个空间,供开发者在安装前展示自己的应用,并提供屏幕截图和文本信息,帮助用户选择安装应用。更丰富的安装界面为 Web 应用开发者提供了一个类似空间,方便开发者直接在浏览器中邀请其用户安装应用。此增强的界面适用于 Chrome(Android 版)和桌面环境。

默认提示

请参阅以下示例了解默认体验,该示例并未提供足够的背景信息。

适用于桌面设备的浏览器默认安装对话框。
桌面设备上的默认安装对话框


适用于移动设备的浏览器默认安装对话框。
移动设备上的默认安装对话框

更丰富的安装界面

如需显示更丰富的安装界面对话框,而不是常规的小默认提示,请向网页清单添加 screenshotsdescription 字段。请查看下面的 Squoosh.app 示例:

在桌面设备和移动设备上提供更丰富的安装界面
在桌面设备和移动设备上提供更丰富的安装界面。

“更丰富的安装界面”对话框由网页清单中的 descriptionscreenshots 字段内容组成。

若要触发该对话框,您只需为相应的设备规格添加至少 1 张屏幕截图,但我们建议您也添加说明。请在下面查看这些字段的具体说明。

屏幕截图

屏幕截图确实能向新的安装界面添加“内容更丰富”的部分,我们强烈推荐使用。在您的清单中添加 screenshots 成员,该成员接受至少需要一张图片的数组,Chrome 最多会显示 8 个图片。相关示例如下所示。

 "screenshots": [
    {
     "src": "source/image1.gif",
      "sizes": "640x320",
      "type": "image/gif",
      "form_factor": "wide",
      "label": "Wonder Widgets"
    }
]

屏幕截图必须符合以下标准:

  • 宽度和高度必须介于 320 像素到 3840 像素之间。
  • 最大尺寸不能超过最小尺寸的 2.3 倍。
  • 所有具有相同外形规格值的屏幕截图都必须具有相同的宽高比
  • 仅支持 JPEG 和 PNG 图片格式。
  • 系统仅会显示 8 张屏幕截图。如果添加了更多标记,用户代理会直接将其忽略。

此外,您还需要添加图片的尺寸和类型,以便图片正确呈现。观看此演示

form_factor 会向浏览器指示屏幕截图应显示在桌面设备 (wide) 还是移动环境 (narrow) 中。

说明

description 成员会在安装提示中描述应用,以邀请用户保留应用。

即使没有 description,该对话框也会显示,但建议使用此对话框。最多在 7 行文字(约 324 个字符)后才开始显示,超出部分的说明会被截断,并且附加省略号(如此示例所示)。

{
…
"description": "Compress and compare images with different codecs
right in your browser."
}
已添加说明
已添加说明。
截断的较长说明。
较长的说明会被截断。

说明会显示在安装提示的顶部。

您可能已经从屏幕截图中注意到,安装对话框还会列出应用的来源。因过长而无法显示界面的源站会被截断(也称为省略),可用作保护用户的安全措施

深入阅读

演示

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="color-scheme" content="dark light" />
    <link rel="manifest" href="manifest.json" />
    <title>How to add Richer Install UI to your web app</title>
    <!-- TODO: Devsite - Removed inline handlers -->
    <!-- <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', () => {
          navigator.serviceWorker.register('sw.js');
        });
      }
    </script>
    <script type="module" src="script.js"></script> -->
  </head>
  <body>
    <h1>How to add Richer Install UI to your web app</h1>
    <ol>
      <li>
        Install the app by clicking the button below. After the installation,
        the button is disabled.
        <p>
          <button disabled type="button">Install</button>
        </p>
      </li>
      <li>
        When you click on install a dialog similar to the ones from app stores
        will be displayed.
      </li>
      <li>
        The dialog includes the `description` and `screenshots` set in the app
        manifest.
      </li>
      <li>
        Screenshots should be different depending if the app is being installed
        on a mobile or desktop device, according to the `form_factor` value set
        for the screenshots on the manifest
      </li>
    </ol>
  </body>
</html>

JS


        // The install button.
const installButton = document.querySelector('button');

// Only relevant for browsers that support installation.
if ('BeforeInstallPromptEvent' in window) {
  // Variable to stash the `BeforeInstallPromptEvent`.
  let installEvent = null;

  // Function that will be run when the app is installed.
  const onInstall = () => {
    // Disable the install button.
    installButton.disabled = true;
    // No longer needed.
    installEvent = null;
  };

  window.addEventListener('beforeinstallprompt', (event) => {
    // Do not show the install prompt quite yet.
    event.preventDefault();
    // Stash the `BeforeInstallPromptEvent` for later.
    installEvent = event;
    // Enable the install button.
    installButton.disabled = false;
  });

  installButton.addEventListener('click', async () => {
    // If there is no stashed `BeforeInstallPromptEvent`, return.
    if (!installEvent) {
      return;
    }
    // Use the stashed `BeforeInstallPromptEvent` to prompt the user.
    installEvent.prompt();
    const result = await installEvent.userChoice;
    // If the user installs the app, run `onInstall()`.
    if (result.outcome === 'accepted') {
      onInstall();
    }
  });

  // The user can decide to ignore the install button
  // and just use the browser prompt directly. In this case
  // likewise run `onInstall()`.
  window.addEventListener('appinstalled', () => {
    onInstall();
  });
}