javascript - iOS 是否支持 "beforeinstallpromp"事件?
<p><p>Android 和桌面版 Chrome 支持“beforeinstallpromp”事件,该事件可以显示添加到主屏幕横幅。我尝试在 iOS 中为我的 PWA 使用相同的 javascript 代码,但它不起作用。 </p>
<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>/**
* Clear caches
*/
function pwaClearCaches()
{
//Clear caches
caches.keys().then(function(names) {
for (let name of names) {
caches.delete(name);
}
});
}
var pwa_app_installed = false; //PWA is already installed
var deferredPrompt; //Link to show dialog event
$(document).ready(function(){
if (window.location.protocol === 'http:') { //Если это HTTP протокол, а не HTTPS
console.log(lang.t('You need HTTPS for work'));
}
if ('serviceWorker' in navigator) {
/**
* Подвешиваемся на переключение режима правки, чтобы сразу очистить кэш
*/
$('.debug-mode-switcher').on('click', function () {
if (!$('.debug-mode-switcher .toggle-switch').hasClass('on')) {
//Delete service worker
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.unregister();
}
});
//Clear caches
pwaClearCaches();
}
});
}
if ($.cookie('update_pwa_cache')){ //Update cache if we have cookie на обновление
pwaClearCaches();
$.cookie('update_pwa_cache', '', {expires: -1});
}
/**
* Close window with prompt
*/
function closePWAInstallWindow()
{
$("#pwaInstall").hide();
$.cookie('not_show_pwa', '1');
}
let body = $('body');
/**
* Add to homescreen event
*/
body.on('click', '#pwaAddToHomeScreen', function(){
deferredPrompt.prompt(); // Show alert to install
deferredPrompt.userChoice.then((choiceResult) => {//Wait for user choose
if (choiceResult.outcome === 'accepted') { //Accept install
closePWAInstallWindow();
} else { //Cansel install
closePWAInstallWindow();
}
deferredPrompt = null;
});
return false;
});
/**
* Close intalll window
*/
body.on('click', '#pwaCloseInstall', function(){
closePWAInstallWindow();
return false;
});
});
console.log('out');
//If we not in webapp and no session that we need to install
if (!(window.matchMedia('(display-mode: standalone)').matches) && !$.cookie('not_show_pwa')) {
/**
* Event that app is installed
*/
$(window).on('appinstalled', (evt) => {
pwa_app_installed = true;
});
console.log('not fired');
/**
* Event beforeinstallprompt from browser
*/
$(window).on('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.originalEvent;
console.log('fired');
var is_mobile_android = false;
var ua = navigator.userAgent;
if (/Android/i.test(ua) && /Chrome/i.test(ua)){ //If we in Android and it is Chrome prevent native window
is_mobile_android = true;
}
if (!pwa_app_installed && !is_mobile_android){
let body = $('body');
body.append('<div id="pwaInstall" class="pwaInstall" style="background-color: #fff">' +
'<div class="content">Please install our app</div><div class="links">' +
'<a href="#" id="pwaAddToHomeScreen" style="background-color: #fff; color: #000;">Add to homescreen</a>' +
'<a href="#" id="pwaCloseInstall" style="background-color: yellow; color: black;">No Thanks</a>' +
'</div>' +
'</div>');
setTimeout(function () { //Show our banner
$("#pwaInstall").addClass('show');
}, 100);
}
});
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script></code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>安装前提示在 iOS Safari 中不可用<br/>
请参阅本页底部的列表<br/>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent" rel="noreferrer noopener nofollow">https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent</a> </p></p>
<p style="font-size: 20px;">关于javascript - iOS 是否支持"beforeinstallpromp"事件?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/55302527/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/55302527/
</a>
</p>
页:
[1]