1

我需要让从应用内 Instagram 浏览器(仅限 Android)安装 PWA 成为可能。我了解应用内浏览器使用标准浏览器文档说支持 AndroidWebview,但实际上它对我不起作用。Lighеhouse - 确认 PWA 中没有错误我附上代码

let deferredPrompt;

const addBtn = document.querySelector('.add-button');
addBtn.style.display = 'none';

window.addEventListener('load', (e) => {
    if ('serviceWorker' in navigator){

        navigator.serviceWorker.register('./sw.js')
            .then(registration => {
                console.log('Service worker successfully registered', registration);
            })
            .catch(error => {
                console.log('Service worker registration failed', error);
            });
    }
});

window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  deferredPrompt = e;
 
  addBtn.style.display = 'block';

  addBtn.addEventListener('click', () => {
    // hide our user interface that shows our A2HS button
    addBtn.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;
    });
  });
});
4

1 回答 1

-1

beforeinstallprompt不幸的是,除非您在 WebView 中做一些额外的工作,否则Android WebView 不支持该事件。

于 2021-01-22T22:55:40.440 回答