ionic android支持版本号,【ionic App问题总结系列】ionic 如何更新app版本

  • Post author:
  • Post category:其他


1 /**2 * App检查更新 Service3 */

4 .factory(‘AppUpdateService’, [“$http”, “Services”, ‘$q’, ‘$cordovaNetwork’, ‘$cordovaAppVersion’, ‘$ionicPopup’, ‘$ionicLoading’, ‘$cordovaFileTransfer’, ‘$cordovaFileOpener2’, ‘$timeout’,5 function($http, Services, $q, $cordovaNetwork, $cordovaAppVersion, $ionicPopup, $ionicLoading, $cordovaFileTransfer, $cordovaFileOpener2, $timeout) {6 return{7 checkVersionData: checkVersionData,8 checkVersion: checkVersion,9 };10

11 /**12 * 调用后端接口获取版本更新信息,这里应该换成你本身的逻辑13 */

14 functioncheckVersionData(data) {15 var deferred =$q.defer();16 $http({method: ‘GET’, url: Services.CHECK_VERSION.url, params: data}).success(function(data) {17 deferred.resolve(data.data);18 }).error(function(err) {19 deferred.reject(err);20 });21 returndeferred.promise;22 }23

24

25 functioncheckVersion(scope) {26 var deferred =$q.defer();27 //params 是我这边须要传递给后端接口的参数,需更改成你本身的参数

28 var params ={29 platform: ‘android’,30 version: ”

31 };32 //获取手机的网络状态,返回的值包括:WIFI CELL_4G CELL_3G等网络状态,这里用来检测手机是否处于WiFi状态

33 var networkType =$cordovaNetwork.getNetwork();34

35 //获取App 内的版本信息

36 $cordovaAppVersion.getVersionNumber().then(function(version) {37 params.version =version;38

39 //获取服务器版本信息,此处需更改成你本身的逻辑

40 checkVersionData(params)41 .then(function(data) {42 //该接口返回的updateFlag取值为:0(不更新)、1(普通更新)、2(强制更新)

43 //判断是否须要更新

44 if(data.updateFlag) {45 var json ={46 title: ”,47 subTitle: data.description48 };49

50 //因为应用内的版本是1.0.0这种格式,因此能够经过正则替换成1.0.0->100,方便进行版本号的比较

51 var nowVersionNum = parseInt(version.toString().replace(new RegExp(/(\.)/g), ‘0’));52

53 //data.version为后端接口返回的须要更新的新版本号

54 var newVersionNum = parseInt(data.version.toString().replace(new RegExp(/(\.)/g), ‘0’));55 if (newVersionNum >nowVersionNum) {56

57 if (data.updateFlag == 1) { //普通更新

58 if (networkType == ‘wifi’) {59 json.title = ‘APP版本更新’

60 }61 else{62 json.title = ‘APP版本更新(建议WIFI下升级)’;63 }64 updateAppPopup(json, scope).then(function(res) {65 if (res == ‘update’) {66 UpdateForAndroid(data.url);67 }68 });69 }70 else if (data.updateFlag == 2 && type == ‘wifi’) { //强制更新

71 UpdateForAndroid(data.url);72 }73 }74 }75 deferred.resolve(data.updateFlag);76 }, function(err) {77 deferred.reject(null);78 })79 });80

81 returndeferred.promise;82 }83

84 functionupdateAppPopup(json, scope) {85 return$ionicPopup.show({86 title: json.title,87 subTitle: json.subTitle,88 scope: scope,89 buttons: [90 {91 text: ‘取消’,92 type: ‘button-clear button-assertive’,93 onTap: function() {94 return ‘cancel’;95 }96 },97 {98 text: ‘更新’,99 type: ‘button-clear button-assertive border-left’,100 onTap: function(e) {101 return ‘update’;102 }103 }104 ]105 });106 }107

108 functionUpdateForAndroid(downloadUrl) {109 $ionicLoading.show({110 template: “已经下载:0%”

111 });112 var targetPath = “/sdcard/Download/tianmicaifu.apk”;113 var trustHosts = true;114 var options ={};115 $cordovaFileTransfer.download(downloadUrl, targetPath, options, trustHosts).then(function(result) {116 $cordovaFileOpener2.open(targetPath, ‘application/vnd.android.package-archive’

117 ).then(function() {118 //成功

119 }, function(err) {120 console.log(err);121 });122 $ionicLoading.hide();123 }, function(err) {124 $ionicLoading.show({125 template: “下载失败”

126 });127 $ionicLoading.hide();128 }, function(progress) {129 $timeout(function() {130 var downloadProgress = (progress.loaded / progress.total) * 100;131 $ionicLoading.show({132 template: “已经下载:” + Math.floor(downloadProgress) + “%”

133 });134 if (downloadProgress > 99) {135 $ionicLoading.hide();136 }137 });138 });139 }140

141 }])