IONIC automatically updates the APP version

 

IONIC automatically updates the APP version

From http://blog.csdn.net/dailuwen/article/details/49887607, I am very grateful to Dai Zi for sharing. For the convenience of reference, I record my own blog.

1. Preparation, add plugins

 

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. If cordova is not added, please execute the following command first  
  2.     1. npm install bower -g  //install bower    
  3.     2. bower install ngCordova    //Install cordova  



 

1.1. Add a plugin for obtaining APP version information

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. ionic plugin add cordova-plugin-app-version  


2.2, add APP automatic update related plug-ins

 

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. ionic plugin add cordova-plugin-file    
  2. ionic plugin add cordova-plugin-file-transfer    
  3. ionic plugin add cordova-plugin-file-opener2  


2. Check the version information when the APP is running (add the following code in the run method)

 

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. .run(function($ionicPlatform, $http, $rootScope, $ionicActionSheet, $timeout, $cordovaAppVersion,    
  2.         $ionicPopup, $ionicLoading, $cordovaFileTransfer, $cordovaFile, $cordovaFileOpener2) {    
  3.     $ionicPlatform.ready(function() {    
  4.         // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard    
  5.         // for form inputs)    
  6.         if (window.cordova && window.cordova.plugins.Keyboard) {    
  7.             cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);    
  8.         };    
  9.         if (window.StatusBar) {    
  10.             StatusBar.styleDefault();    
  11.         };    
  12.         //服务器上保存版本信息  
  13.         $http.get('http://localhost/app/ver.json')    
  14.         .then(function(data){    
  15.             var serverAppVersion = data.data.verInfo;//服务器 版本    
  16.             console.log("====>>服务器"+serverAppVersion);    
  17.             $cordovaAppVersion.getVersionNumber().then(function(version) {    
  18.                 console.log("version=====本机>>>"+version+"====>>服务器"+serverAppVersion);    
  19.                 if (version != serverAppVersion) {    
  20.                     $ionicLoading.show({    
  21.                         template: "已经下载:0%"    
  22.                     });    
  23.                     var url = "http://192.168.1.77:8080/app/android-debug.apk";     
  24.                     var targetPath = "file:///mnt/sdcard/Download/android-debug.apk";     
  25.                     var trustHosts = true    
  26.                     var options = {};    
  27.                     $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {    
  28.                         $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'    
  29.                         ).then(function () {    
  30.                             }, function (err) {    
  31.                             });    
  32.                         $ionicLoading.hide();    
  33.                     }, function (err) {    
  34.                         alert('下载失败');    
  35.                     }, function (progress) {                               
  36.                         $timeout(function () {    
  37.                             var downloadProgress = (progress.loaded / progress.total) * 100;    
  38.                             $ionicLoading.show({    
  39.                                 template:  "Downloaded:"  + Math.floor(downloadProgress) +  "%"    
  40.                             });    
  41.                             if (downloadProgress > 99) {    
  42.                                 $ionicLoading.hide();    
  43.                             }    
  44.                         })    
  45.                     });    
  46.                 }    
  47.             });    
  48.         });    
  49.             
  50.     });    
  51.         
  52. })  


3. Add the following two files under the server webapp

 

 

 

[java]  view plain copy  
 
 print ? View code snippets on CODE Derive to my code slice
  1. 1、ver.json  
  2. 2、Android-debug.apk  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361545&siteId=291194637