开发项目的时候自动更新工鞥呢,需要获取app的版本,cordova-plugin-app-version可以用来的获取版本号,可以用命令行:cordova plugin add cordova-plugin-app-version,或者在配置文件config.xml、package.json进行配置。
clipboard.png

clipboard.png

 getServerVersion: function (curVersion) {
        var deferred = $q.defer(),
          promise;

        if (curVersion) {
          promise = $http.get(CONFIG.url.version, {params: {version: curVersion}});
        } else {
          promise = $http.get(CONFIG.url.version);
        }
        promise.then(function (response) {
          deferred.resolve(response.data.data.versions);
        }).catch(function (error) {
          deferred.reject(error);
        });
        return deferred.promise;
      },

      getLocalVersion: function () {
        var deferred = $q.defer();

        cordova.getAppVersion.getVersionNumber().then(function (localVersion) {
          deferred.resolve(localVersion);
        }).catch(function (error) {
          deferred.reject(error);
        });
        return deferred.promise;
      },

      checkUpdate: function () {
        var self = this,
          localVersion;

        this.getLocalVersion().then(function (response) {
          localVersion = response;
          return self.getServerVersion(response);
        }).then(function (serverVersions) {
          var latestVersion = _.isArray(serverVersions) ? _.first(serverVersions) : serverVersions,
            laterThanServer = commonService.laterThan(localVersion, latestVersion.number),
            isForceUpdate = _.some(_.initial(serverVersions), function (item) {
              return item.flag === 1;
            });

          commonService.setItem("latestVersion", latestVersion);
          return laterThanServer && isForceUpdate;
        }).then(function (isForce) {
          isForce && commonService.confirm(i18n.need_update_label, i18n.force_update_tip).then(function (res) {
            if (res) {
              checkUpdateEnv(isForce);
            } else {
              ionic.Platform.exitApp();
            }
          });
        }).catch(function (reason) {
          console.log("version compare filed : " + JSON.stringify(reason));
        });
      }

但是有一点需要注意的是执行此语句,必须等待DOM元素加载完毕。简单一点就是,若是在传统的javaScript中用到,需要配合着window.onload使用,或者 ionic.Platform.ready中

ionic.Platform.ready(function () {
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
        window.cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
        window.cordova.plugins.Keyboard.disableScroll(true);
        apiService.checkUpdate();
      }
      try {
        $cordovaStatusbar.style(0);
        checkNetworkState();
        checkSystemVersion();
        $ionicPlatform.is("Android") && $ionicPlatform.registerBackButtonAction(registerBackButton, 101);
      } catch (e) {
        console.warn("some thing error when app launched: " + e.message);
      }
      autoLogin();
    });
相关文章:

http://www.ionic.ren/2015/11/...


嘉恩Gavin
98 声望5 粉丝

[链接]