ionic版本更新

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


解决思路:


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


解决步骤:


① 安装好ionic开发环境


② 安装相关插件


版本信息:cordova plugin addcordova-plugin-app-version    0.1.9

文件管理:cordova plugin add cordova-plugin-file            5.0.0

文件传输:cordova plugin addcordova-plugin-file-transfer    1.7.1

打开文件:cordova plugin addcordova-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('Storagepermission 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('checkdataDirectory');

               $cordovaFile.createDir("file:///storage/emulated/0/","download", false)

                    .then(function (success) {

                        // success

                        userDate(url,targetPath)


                    }, function (error) {

                        // error

                    });


           });

       }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容