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. Flask扩展 -- flask-mail

    电子邮件是最常用的通信方式之一.虽然Python标准库中的smtplib包可用在Flask程序中发送电子邮件,但包装了smtplib的Flask-Mail扩展能更好的和Flask集成. 1.安装Fla ...

  2. 在Rancher2中部署Docker-Registry仓库服务

    1.docker登陆认证 [root@master ~]# vim /etc/docker/daemon.json { "insecure-registries": [" ...

  3. HashMap(常用)方法个人理解

      Hashmap的存值: public static void main(String[] args) { ///*Integer*/map.put("1", 1);//向map ...

  4. win 7 下安装GIT(亲测有效)

    我首先是百度到了这个网站:https://git-scm.com/download/win 当然由于外网访问速度的缓慢 可以直接在百度搜索下载自己对应的版本 这个网站上有下载链接,你可以根据你的系统选 ...

  5. SQLServer日期格式化及创建相关日期

    DECLARE @FirstDay_M DATETIME --本月初日期 ,); DECLARE @LastDay_M DATETIME --本月末日期 SET @LastDay_M = DATEAD ...

  6. (转) Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7

    Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7 转自 http://blog.csdn.net/skykingf/article/details/17450561 http:// ...

  7. C#winfrom listview 设置显示图片

    ListView控件有5种显示图片方式:LargeIcon(大图标),Detail(详细),SmallIcon(小图标),List(列表),Tile,常用前4种.  这里说一下设置方式:LargeIc ...

  8. 12_springmvc拦截器

    一.定义 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 二.拦截器定义 实现HandlerInterceptor接口, ...

  9. 04.Mybatis输出映射之ResultMap

    当实体类中的字段名与数据库中的字段名不一致时需要手动设置映射关系 在Mapper.xml中定义 <!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo ...

  10. Loadrunner安装与破解【转】

    Loadrunner安装与破解 一.下载 我的LoadRunner 11下载地址是: http://pan.baidu.com/s/1qYFy2DI 二.安装 1.启动安装程序 运行setup.exe ...