1.安装相关插件

cordova-plugin-file  ,cordova-plugin-file-opener2,cordova-plugin-file-transfer,cordova-plugin-app-version

cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-file-opener2
cordova plugin add cordova-plugin-file-transfer

2.html

<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 插件

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.后端代码

/// <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("", "获取成功", "Result", datas);
}
else
{
strjson = CommonMenthod.GetResultStr("", "当前是最新版本,不需要更新");
} 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配置

备注:修改大于当前版本号和配置下载app路径相关内容就可以更新了

7.IIS配置

8效果图

 

在android 7.0系统上导致无法下载更新是权限问题,修改下载路径

cordova.file.externalDataDirectory + "download/memberApp.apk" ;

Ionic App 更新插件cordova-plugin-app-version的更多相关文章

  1. ionic 项目内部更新用到的插件,退出app插件

    一 cordova-plugin-app-version插件  用来获取APP版本 ionic plugin add cordova-plugin-app-version -----cordova-p ...

  2. cordova热更新插件的使用

    cordova常用命令  一.添加插件 新建Cordova项目 cordova create CordovaHotCode com.ezample.hotcode 添加Android平台 在新建项目的 ...

  3. cordova获取app版本信息插件的使用:cordova-plugin-app-version

    1. 添加插件: cordova plugin add cordova-plugin-app-version 2. 调用方法: //获取当前文件的版本号: document.addEventListe ...

  4. ionic cordova plugin simple demo

    要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...

  5. cordova 更改app的图标

    写在前面:cordova 使一个前端开发者成为一个“假”的android开发人员,不得不说提供给我们巨大的方便~,cordova打包生成的apk的默认样式和启动的名字真的是需要我们字更改的:本文将记录 ...

  6. 使用 Cordova 打包 app

    1.安装nodejs 2.安装 cordova npm install -g cordova 3.Cordova 打包成安卓APK需要用到ANT打包工具,首先配置好java环境: 下载安装Java J ...

  7. 从国内APP更新“精雕细琢” 看国内外产品理念之差

    看国内外产品理念之差" title="从国内APP更新"精雕细琢" 看国内外产品理念之差"> 对于当下的大众来说,智能手机已经成为新的" ...

  8. cordova plugin汇总大全

    1.获取当前应用的版本号 cordova plugin add cordova-plugin-app-version 2.获取网络连接信息 cordova plugin add cordova-plu ...

  9. ionic 热更新 cordova-hot-code-push

    cordova-hot-code-push ,Cordova热代码推送插件提供了在应用程序中执行基于Web的内容的自动更新的功能.使用此插件可以更新存储在项目的www文件夹中的所有内容. cordov ...

随机推荐

  1. 【转载】99%的人都理解错了HTTP中GET与POST的区别

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文链接 作者:WebT ...

  2. linux操作练习题

    linux操作练习题 一.总结 一句话总结: 多练练一下子就会了,很简单的 1.在当前目录下建立文件exam.c,将文件exam.c拷贝到/tmp这个目录下,并改名为 shiyan.c? touch ...

  3. Socket.EndReceive 方法 (IAsyncResult)

        .NET Framework (current version)   其他版本 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 . ...

  4. 伪造ip头

    x-forwarded-for: 127.0.0.1x-remote-IP: 127.0.0.1x-remote-ip: 127.0.0.1x-client-ip: 127.0.0.1x-client ...

  5. input 不显示输入的历史记录

    第一次在 input 输入后,下次就会自动显示输入历史记录,去掉这种默认效果的解决方案 <input name="username" type="text" ...

  6. JS获取url参数,修改url参数

    function getURL(){ var args = {}; var query = location.search.substring(1); //获得了当前链接的中?号后的参数 var pa ...

  7. Luogu P2484 [SDOI2011]打地鼠(模拟+前缀和)

    P2484 [SDOI2011]打地鼠 题意 题目描述 打地鼠是这样的一个游戏:地面上有一些地鼠洞,地鼠们会不时从洞里探出头来很短时间后又缩回洞中.玩家的目标是在地鼠伸出头时,用锤子砸其头部,砸到的地 ...

  8. Codeforces 548E Mike ans Foam (与质数相关的容斥多半会用到莫比乌斯函数)

    题面 链接:CF548E Description Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a sp ...

  9. half adder vs. full adder

    1, half adder 2-input, 2-output input: A, B; output out, carry; 2, full adder 3-input, 2-output inpu ...

  10. day09 samba、nginx服务配置

    samba 1.环境准备 [root@localhost ~]# iptables -F #清除防火墙配置 [root@localhost ~]# systemctl stop firewalld # ...