1.安裝相關插件html
cordova-plugin-file ,cordova-plugin-file-opener2,cordova-plugin-file-transfer,cordova-plugin-app-versionandroid
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-file-opener2
cordova plugin add cordova-plugin-file-transferios
2.htmljson
<ion-view view-title="關於咱們" > <ion-content overflow-scroll="true" style="overflow: hidden"> <ion-list> <ion-item>******科技股份有限公司</ion-item> <ion-item>地址:北京市海淀區**********</ion-item> <ion-item>郵箱:sulin11026@163.com</ion-item> <ion-item>電話: *********</ion-item> <ion-item><sapn>版本:{{versionNow}}</sapn>
<span style="float:right"><button class="button button-balanced" ng-click="UpdateApp()">檢查更新</button></span></ion-item> </ion-list> </ion-content> </ion-view>
3.controller後端
.controller('AboutCtrl', function ($scope, ENV, $http, Storage, $ionicLoading, $ionicPopup, $timeout,$cordovaFileTransfer, $cordovaFileOpener2,CommonFactory) { $scope.versionNow= ENV.version; $scope.$on('$ionicView.beforeEnter', function () { // var versionType = navigator.platform; //獲取操做系統Android ios if (Storage.getLoginInfo() != null) { $scope.user_name = Storage.getLoginInfo().user_name; $scope.userId = Storage.getLoginInfo().id; } $scope.versionNow = ENV.version; }) // 查詢最新版本下載並安裝app $scope.UpdateApp = function () { var versioninter=ENV.versionInter; CommonFactory.NewVersion(versioninter).then(function(data){ if (data){ var json =JSON.parse(data); if(json.status==1){ var versionmodel=json.Result; var confirmPopup = $ionicPopup.confirm({ title: versionmodel.Title, template: versionmodel.Content, cancelText: '下次再說', okText: '當即更新' }); confirmPopup.then(function (res) { if (res) { $ionicLoading.show({ template: "已經下載:0%" }); var url = encodeURI(versionmodel.ApkAddress); var targetPath = cordova.file.externalRootDirectory + "/download/" + url.substr(url.lastIndexOf("/") + 1); var trustHosts = true var options = {}; $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) { // 打開下載下來的APP $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive' ).then(function () { // 成功 }, function (err) { // 錯誤 }); $ionicLoading.hide(); }, function (err) { $ionicLoading.show({ noBackdrop: true, template: "下載失敗,請檢查網絡", duration: 1500 }); }, function (progress) { //進度,這裏使用文字顯示下載百分比 $timeout(function () { var downloadProgress = (progress.loaded / progress.total) * 100; $ionicLoading.show({ template: "已經下載:" + Math.floor(downloadProgress) + "%" }); if (downloadProgress > 99) { $ionicLoading.hide(); } }) }); } else { // 取消更新 } }); }else{ $ionicLoading.show({ noBackdrop: true, template: json.msg, duration: 1500 }); } } }); } }) ;
備註:$cordova開頭都是ngcordova 插件 須要引用ngCordova ,不然就是cordova 插件promise
4.service網絡
.factory("CommonFactory", function ($q, ENV, $http) { var getNewVersion=function(vsion){ var r = $q.defer(); $http.get(ENV.APIUrl + "/About/GetNewVersion", { params: { strverersion: vsion } }).success(function (data) { r.resolve(data); }).error(function (er) { r.resolve(!1); }); return r.promise; } return { NewVersion:getNewVersion } })
5.後端代碼app
/// <summary> /// 獲取最新版本 /// </summary> /// <returns></returns> [HttpGet] public string GetNewVersion(string strverersion) { string strjson = ""; string datas = ""; string xmlpath = HostingEnvironment.MapPath("~/xmlconfig/verapp.config"); XDocument xelement = XDocument.Load(xmlpath); VersionModel vsmodel = new VersionModel(); foreach (var item in xelement.Element("appconfig").Elements()) { switch (item.Name.ToString().Trim()) { case "title": vsmodel.Title = item.Value.ToString().Trim(); break; case "content": vsmodel.Content = item.Value.ToString().Trim(); break; case "version": vsmodel.Version = item.Value.ToString().Trim(); break; case "apk": vsmodel.ApkAddress = item.Value.ToString().Trim(); break; default: break; } }; if (vsmodel.Version!="" && int.Parse(vsmodel.Version)> int.Parse(strverersion)) { datas = JsonConvert.SerializeObject(vsmodel); strjson = CommonMenthod.GetResultStr("1", "獲取成功", "Result", datas); } else { strjson = CommonMenthod.GetResultStr("0", "當前是最新版本,不須要更新"); } return strjson; } public class VersionModel { public string Version { get; set; } public string Title { get; set; } public string Content { get; set; } public string ApkAddress { get; set; } } public static string GetResultStr(string status, string msg,string datakey,string datainfo) { datainfo = datainfo == "" ? "[]" : datainfo; string strjson = "{\"status\":" + status + ",\"msg\":\""+ msg + "\",\""+ datakey + "\":"+ datainfo + "}"; return strjson; }
6 xml配置ionic
備註:修改大於當前版本號和配置下載app路徑相關內容就能夠更新了ide
7.IIS配置
8效果圖
在android 7.0系統上致使沒法下載更新是權限問題,修改下載路徑
cordova.file.externalDataDirectory + "download/memberApp.apk" ;