1.獲取版本號 須要添加 插件 node
js 進行調用:cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git
// 獲取當前移動設備已經安裝的版本 cordova.getAppVersion.getVersionNumber().then(function (version) { var versionCode = parseInt(version.toString().replace(/\./g,'')); console.log(versionCode); // 1.獲取當前版本號 // 2.獲取服務器端的最新版本的數據源 // 3.進行版本比較,若是當前的版本號與服務器的版本號不一致時,下載並安裝最新的應用程序安裝包 Common.alert("舒適提示", "有新版本可供更新.\n 1.界面美化 \n 2.性能優化\n 3.最新版本:"+versionCode); });
2. 使用ajax進行獲取服務中最新的安裝包的詳細信息android
3.比較版本號是否一致,若是不一致,將會提示是否進行「更新」git
4.若是選擇"更新",對執行安裝包的下載github
var url = "http://ftp-apk.pconline.com.cn/5014afca744eddb3fc7fe2d25d60aa29/pub/download/201010/HiMarket.apk?crazycache=1"; //能夠從服務端獲取更新APP的路徑 var targetPath = "HiMarket.apk"; //APP下載存放的路徑,可使用cordova file插件進行相關配置 var trustHosts = true; var options = {}; var ft = new FileTransfer(); ft.download(url, targetPath, successCallback, fileTransfer.downloadFailed, trustHosts, options);
5.將打開下載後的安裝包ajax
Android 平臺下打開安裝包文件的方式與iOS的方式不同api
Android使用本地路徑進行打開promise
iOS須要進行跳轉到app store 中進行下載更新性能優化
此時須要將FileOpener2.h中定義的方法添加到plugins.FileOpener2.js (iOS項目中的cordova-plugin-file-opener2目錄下的www目錄中)服務器
cordova.define("cordova-plugin-file-opener2.FileOpener2", function(require, exports, module) { /*jslint browser: true, devel: true, node: true, sloppy: true, plusplus: true*/ /*global require, cordova */ var exec = require('cordova/exec'); function FileOpener2() {} FileOpener2.prototype.openURL = function (url, callbackContext) { callbackContext = callbackContext || {}; exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'openURL', [url]); }; FileOpener2.prototype.open = function (fileName, contentType, callbackContext) { callbackContext = callbackContext || {}; exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType]); }; FileOpener2.prototype.uninstall = function (packageId, callbackContext) { callbackContext = callbackContext || {}; exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'uninstall', [packageId]); }; FileOpener2.prototype.appIsInstalled = function (packageId, callbackContext) { callbackContext = callbackContext || {}; exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'appIsInstalled', [packageId]); }; module.exports = new FileOpener2(); });
6.將這兩種文件的打開方式進行封裝成FileOpen.jsapp
/** * 使用方法: * fileOpener.open(url).then(function(){}); * 用到的原生api 有:cordova-plugin-dialogs,cordova-plugin-device,cordova-plugin-FileTransfer */ /**FileTransfer*/ var ft; Ext.define("util.FileOpen", { alternateClassName: "fileOpener", singleton: true, config: { isComplete: false, description: '高視醫療信息' }, constructor: function (config) { this.initConfig(config); }, // Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS getPromisedExec: function (command, success, fail) { var toReturn, deferred, injector, $q; if (success === undefined) { if (window.jQuery) { deferred = jQuery.Deferred(); success = deferred.resolve; fail = deferred.reject; toReturn = deferred; } else if (window.angular) { injector = angular.injector(["ng"]); $q = injector.get("$q"); deferred = $q.defer(); success = deferred.resolve; fail = deferred.reject; toReturn = deferred.promise; } else if (window.Promise) { toReturn = new Promise(function (c, e) { success = c; fail = e; }); } else if (window.WinJS && window.WinJS.Promise) { toReturn = new WinJS.Promise(function (c, e) { success = c; fail = e; }); } else { return console.error('AppVersion either needs a success callback, or jQuery/AngularJS/Promise/WinJS.Promise defined for using promises'); } } if (device.platform == "Android") { cordova.plugins.fileOpener2.open(command, 'application/vnd.android.package-archive', success, fail); } else if (device.platform == "iOS") { // 加載其餘應用,應用在升級的時候,iOS使用openURL打開、Android使用open打開 'tel://10086' cordova.plugins.fileOpener2.openURL(command, { success: success, error: fail }); } return toReturn; }, open: function (filePath, success, fail) { return this.getPromisedExec(filePath, success, fail); } });
使用方法:
1.比較版本號
cordova.getAppVersion.getVersionNumber().then(function (version) { var versionCode = parseInt(version.toString().replace(/\./g,'')); // 此處寫比較版本號邏輯,須要獲取服務器上應用程序的最新版本號 });
2.下載安裝包,並執行安裝
Progress.start("正在下載,請稍後..."); // 下載安裝包 fileTransfer.download(function(entry){ Progress.close(); // 打開已經下載的安裝包 fileOpener.open(entry.fullPath).then(function(message){ console.log(message); },function(message){ console.log(message); }); console.log('download complete: ' + entry.fullPath); console.log('download name: ' + entry.name); });