نحوه ایجاد میانبر برنامه

فرانسوا بوفور
François Beaufort

میانبرهای برنامه به کاربران کمک می کند تا به سرعت کارهای رایج یا توصیه شده را در برنامه وب شما شروع کنند. دسترسی آسان به آن وظایف از هر جایی که نماد برنامه نمایش داده می شود، بهره وری کاربران را افزایش می دهد و همچنین تعامل آنها با برنامه وب را افزایش می دهد.

روش مدرن

میانبرهای برنامه را در مانیفست برنامه وب تعریف کنید

منوی میانبرهای برنامه با کلیک راست روی نماد برنامه در نوار وظیفه (ویندوز) یا داک (macOS) روی دسکتاپ کاربر، یا با لمس و نگه داشتن نماد راه‌انداز برنامه در اندروید، فراخوانی می‌شود.

منوی میانبرهای برنامه باز شده در Android.
منوی میانبرهای برنامه در اندروید باز شد
منوی میانبرهای برنامه باز شده در ویندوز.
منوی میانبرهای برنامه در ویندوز باز شد

منوی میانبرهای برنامه فقط برای برنامه های وب پیشرفته نصب شده نشان داده می شود. برای آشنایی با الزامات قابلیت نصب ، نصب را در ماژول Learn 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
  • ایکس
  • 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();
  });
}