重新打开应用程序时,iOS 12中的PWA不再重新执行Javascript

时间:2019-05-11 21:10:45

标签: javascript html ios progressive-web-apps

我有一个PWA,它在使用Javascript打开时实质上将用户重定向到消息应用程序。随着iOS 12的推出以及对PWA的更改,网页在重新打开或重新获得焦点时不再重新初始化并执行Javascript。相反,它现在加载以前保存的状态,并且不会重新执行Javascript。

有人对此有任何想法吗?每当PWA关注时,我可以强制执行Javascript吗?我可以强制页面在加载时重新初始化吗?

下面的示例代码:

<html manifest="ios/scripts/offline.manifest">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>SMS</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-title" content="SMS">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />


<link rel="apple-touch-icon" sizes="180x180" href="ios/img/Icon-60x60@3x.png">
<link rel="apple-touch-startup-image" href="ios/img/LaunchImage-1125@3x~iphoneX-portrait_1125x2436.png" media="(device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait)">
</head>

<body>
<script>
        if (window.navigator.standalone) {
            document.write('<a id="url" href="sms:1111111111" name="url"></a>');
            var e = document.getElementById('url');
            var ev = document.createEvent('MouseEvents');
            ev.initEvent('click', true, true);
            e.dispatchEvent(ev);
        }
</script>
</body>

</html>


2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。这是我强迫PWA在开始时执行javascript的操作:

  1. 在嵌入的javascript中注册页面可见性事件 标签之间。
  2. 在页面可见性事件的事件处理程序中,执行随后打开PWA时要执行的javascript。

每次打开PWA都会触发页面可见性事件并执行脚本。

示例:在您的html文件中:

<script>
registerPageVisibility()
</script>

function registerPageVisibility() {
   
    let hidden;
    let visibilityChange;
    if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
        hidden = 'hidden';
        visibilityChange = 'visibilitychange';
    } else if (typeof document.msHidden !== 'undefined') {
        hidden = 'msHidden';
        visibilityChange = 'msvisibilitychange';
    } else if (typeof document.webkitHidden !== 'undefined') {
        hidden = 'webkitHidden';
        visibilityChange = 'webkitvisibilitychange';
    }
    window.document.addEventListener(visibilityChange, () => {
        if (!document[hidden]) {
            //put your script here and it will be execute everytime when PWA is opened.
            
        }
    });
}

答案 1 :(得分:0)