앱 바로가기를 만드는 방법

François Beaufort
François Beaufort

앱 바로가기를 사용하면 사용자가 웹 앱 내에서 자주 실행되거나 권장되는 작업을 빠르게 시작할 수 있습니다. 앱 아이콘이 표시된 곳이라면 어디서나 이러한 작업에 쉽게 액세스할 수 있어 사용자의 생산성이 향상되고 웹 앱에 대한 사용자의 참여도가 높아집니다.

현대적인 방식

웹 앱 매니페스트에서 앱 바로가기 정의

앱 바로가기 메뉴는 사용자 바탕화면의 작업 표시줄 (Windows) 또는 도크 (macOS)에서 앱 아이콘을 마우스 오른쪽 버튼으로 클릭하거나 Android에서 앱의 런처 아이콘을 길게 터치하여 호출됩니다.

Android에서 앱 바로가기 메뉴가 열려 있습니다.
Android에서 앱 바로가기 메뉴가 열림
Windows에서 열린 앱 바로가기 메뉴
Windows에서 앱 바로가기 메뉴가 열림

앱 바로가기 메뉴는 설치된 프로그레시브 웹 앱에만 표시됩니다. PWA 알아보기 모듈에서 설치를 확인하여 설치 가능 요건을 알아보세요.

각 앱 바로가기는 사용자 인텐트를 표현하며, 각 인텐트는 웹 앱 범위 내의 URL과 연결되어 있습니다. 사용자가 앱 바로가기를 활성화하면 URL이 열립니다.

앱 바로가기는 웹 앱 매니페스트shortcuts 배열 멤버에서 선택적으로 선언됩니다. 다음은 잠재적인 웹 앱 매니페스트의 예입니다.

{
  "name": "Player FM",
  "start_url": "https://player.fm?utm_source=homescreen",
  "shortcuts": [
    {
      "name": "Open Play Later",
      "short_name": "Play Later",
      "description": "View the list of podcasts you saved for later",
      "url": "/play-later?utm_source=homescreen",
      "icons": [{ "src": "/icons/play-later.png", "sizes": "192x192" }]
    },
    {
      "name": "View Subscriptions",
      "short_name": "Subscriptions",
      "description": "View the list of podcasts you listen to",
      "url": "/subscriptions?utm_source=homescreen",
      "icons": [{ "src": "/icons/subscriptions.png", "sizes": "192x192" }]
    }
  ]
}

브라우저 지원

  • 96
  • 96
  • x
  • 17.4

소스

기존의 방식

앱이 아직 설치되지 않은 경우 사용자에게 웹페이지에서 일부 링크를 드래그하여 브라우저 북마크바에 드래그 앤 드롭하도록 제안할 수 있습니다. 이렇게 하면 웹 앱 내에서 자주 수행되거나 권장되는 작업을 빠르게 시작할 수 있습니다.

추가 자료

데모

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 create app shortcuts</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 create app shortcuts</h1>
    <ol>
      <li>
        You can drag these <a href="blue.html">blue page</a> or
        <a href="red.html">red page</a> links to the bookmarks bar
        and access them later.
      </li>
      <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>
    </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();
  });
}