ionic 版本更新

问题描述:启动APP时检测是否有新版本需要更新,提示进行更新

解决思路:

1.从服务器获取版本号,与当前app版本的版本号进行比较,若不相同,则进行更新,相同直接跳过,进入登录界面

解决步骤:

① 安装好ionic开发环境

② 安装相关插件

版本信息:cordova plugin add cordova-plugin-app-version 0.1.9
文件管理:cordova plugin add cordova-plugin-file 5.0.0
文件传输:cordova plugin add cordova-plugin-file-transfer 1.7.1
打开文件:cordova plugin add cordova-plugin-file-opener2 2.0.19
权限插件:cordova pluign add cordova-plugin-android-permissions 1.0.0
安装权限插件主要是因为Android8.0以上的版本有权限限制。

③相关代码:app.js
权限代码:
允许读写权限,打开之后可以下载并且打开Apk
function userDate(url, targetPath){
var permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.READ_EXTERNAL_STORAGE, checkPermissionCallback, null);
function checkPermissionCallback(status) {
if (!status.hasPermission) {
var errorCallback = function() {
console.log(‘Storage permission is not turned on’);
}
permissions.requestPermission(
permissions.READ_EXTERNAL_STORAGE,
function(status) {
if (!status.hasPermission) {
errorCallback();
} else {
// download_app();
download_app(url, targetPath)
}
},
errorCallback);
}else{
download_app(url, targetPath)
}
}
}

版本更新: 通过得到apk得版本号,与后台得到的版本号进行对比。
function check_update(event) {
//获取apk版本号
$cordovaAppVersion.getVersionNumber().then(function (version) {
$http.get('http://localhost:8080/api/v2/version_update/get_version ').then(function (result) {
var options = {
location: ‘no’,
clearcache: ‘yes’,
toolbar: ‘yes’
};
//版本号对比
if (result.data.version_name > version) {
var link = result.data.url_version //下载路径
var ver = result.data.version_name //版本号
var confirmsPopup = $ionicPopup.confirm({
title: ‘检测更新’,
template: ‘已发现新版本’ + ver,
okText:“确定”,
cancelText:“取消”
});
confirmsPopup.then(function(res) {
if(res) {
check(link,“file:///storage/emulated/0/download/envpollut.apk”)
} else {

              }
            });

        } else {
            console.log(result);
        }
       })
    })
     
  }
 下载apk调用方法,
  function download_app(url, targetPath) {
        $ionicLoading.show({
            template: "正在下载中......"
        });
        $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
            $ionicLoading.hide();
            alert('下载完成');
            //打开Apk
            $cordovaFileOpener2.open(
                targetPath,
                'application/vnd.android.package-archive'
            ).then(function () {
                alert('打开安装');

            }, function (err) {
                 alert('安装失败');
            });

        }, function (err) {
            // Error
            $ionicLoading.show({
                template: "下载失败"
            });
            $ionicLoading.hide();
        }, function (progress) {
            var downloadProgress = (progress.loaded / progress.total) * 100;
            if (downloadProgress > 99) {
                $ionicLoading.hide();
            }
        });
    }

检查apk是否存在
function check(url, targetPath) {
$cordovaFile.checkDir(“file:///storage/emulated/0/”, “download”).then(function (success) {
userDate(url, targetPath)
}, function (error) {
// 不存在则创建
$log.error(‘check dataDirectory’);
$cordovaFile.createDir(“file:///storage/emulated/0/”, “download”, false)
.then(function (success) {
// success
userDate(url, targetPath)

                }, function (error) {
                    // error
                });

        });
    }

Config.xml中 platform name =”android”下


注:bug
a. 安装插件之后,对应的ionic1版本的话,安装之后会造成不能打包,需要在安装插件时 file插件对应版本号安装,添加Android平台时需要cordova platform add [email protected],
此时需要查看平台是否添加完整。
b. 为了解决权限问题,添加权限之后需要在AndroidMifeat
固定版本号

权限查看
















c. 如果build时遇到transform…错 在Android平台下build.gradle
configurations.all {
resolutionStrategy {
force ‘com.android.support:support-v4:27.1.0’
}
}
def promptForReleaseKeyPassword()之前添加

猜你喜欢

转载自blog.csdn.net/weixin_43806483/article/details/84560510