Angular Service Workers无法在iOS独立模式下工作

时间:2019-04-08 09:24:10

标签: ios angular progressive-web-apps ionic4

我正在与Ionic v4和Angular PWA服务人员一起进行PWA。在app.component.ts上提供了一项注入服务,以检查PWA上是否有更新,然后下载并重新加载。

当我使用浏览器打开PWA时,服务运行良好;如果部署更新,则服务工作程序将更新缓存的PWA并“自动重新加载”页面。

问题与PWA /独立模式有关,在iOS上,当PWA添加到主屏幕时,它不再刷新,甚至用任务应用列表将其杀死。仅当我关闭设备并再次打开时,它才会刷新应用程序的状态。在Android设备上,所有功能都可以在PWA模式下正常工作。

这是我与Angular Service Worker一起实施的版本检查器服务:

import {Injectable} from '@angular/core';
import {SwUpdate} from '@angular/service-worker';

@Injectable()
export class LogUpdateService {

    constructor(updates: SwUpdate) {
        console.warn('check')
        updates.available.subscribe(event => {
            console.log('current version is', event.current);
            console.log('available version is', event.available);
            updates.activateUpdate().then(() => {
                console.warn('downloaded')
                document.location.reload();
            });
        });
        updates.activated.subscribe(event => {
            console.log('old version was', event.previous);
            console.log('new version is', event.current);
        });
    }
}

这是我的package.json:

"dependencies": {
    "@angular/common": "^7.2.9",
    "@angular/core": "^7.2.9",
    "@angular/forms": "^7.2.9",
    "@angular/http": "^7.2.9",
    "@angular/platform-browser": "^7.2.9",
    "@angular/platform-browser-dynamic": "^7.2.9",
    "@angular/pwa": "0.13.8",
    "@angular/router": "^7.2.9",
    "@angular/service-worker": "7.2.12",
    "@ionic-native/core": "^5.2.0",
    "@ionic-native/network": "^5.2.0",
    "@ionic-native/splash-screen": "^5.2.0",
    "@ionic-native/status-bar": "^5.2.0",
    "@ionic/angular": "4.2.0",
    "@ionic/storage": "^2.2.0",
    "@ngx-translate/core": "^11.0.1",
    "@ngx-translate/http-loader": "^4.0.0",
    "cordova-plugin-network-information": "2.0.1",
    "cordova-sqlite-storage": "3.2.0",
    "core-js": "^2.6.5",
    "ionic2-calendar": "^0.5.2",
    "moment": "^2.24.0",
    "rxjs": "~6.4.0",
    "serve": "^10.1.2",
    "zone.js": "~0.8.29"
  },
  "devDependencies": {
    "@angular-devkit/architect": "0.13.8",
    "@angular-devkit/build-angular": "0.13.8",
    "@angular-devkit/core": "7.3.8",
    "@angular-devkit/schematics": "7.3.8",
    "@angular/cli": "7.3.8",
    "@angular/compiler": "~7.2.9",
    "@angular/compiler-cli": "~7.2.9",
    "@angular/language-service": "~7.2.9",
    "@ionic/angular-toolkit": "1.5.0",
    "@types/jasmine": "~3.3.9",
    "@types/jasminewd2": "~2.0.6",
    "@types/node": "~11.11.3",
    "@ionic/lab": "1.0.24",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~3.3.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.5",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.2",
    "ts-node": "~8.0.3",
    "tslint": "~5.14.0",
    "typescript": "^3.1.6"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-sqlite-storage": {},
      "cordova-plugin-network-information": {}
    }
  }

有这个问题吗?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我发现的唯一解决方案是对将执行以下操作的javascript函数(例如updateiOS())进行编码

1-验证我们是否在iOS独立模式下运行(如果没有,则不要继续)

2-如果发现新版本,请在服务器上检查新版本并重新加载页面(确保未缓存此https请求!)

3-将上次检查时间保存在变量中(例如var lastCheckTime)

在我的主要javascript函数中,我将为“ focus”事件创建一个侦听器,该事件将检查now和lastCheckTime之间的差异,如果超过1天,请致电updateiOS()

到目前为止,焦点活动对我来说还不错