ionic开发APP更新方法以及打包(Android && IOS)

Android更新原理

         1、获取服务器端的APP版本
     2、获取当前版本
     3、判断两个版本是否相等,不相等提示更新,否则不更新

准备cordova插件

 cordova plugin add cordova-plugin-app-version  // 获取APP版本
 cordova plugin add cordova-plugin-file // 文件系统
 cordova plugin add cordova-plugin-file-transfer  //文件传输系统
 cordova plugin add cordova-plugin-file-opener2  //文件打开系统

更新方法步骤:

第一步:在app.js的run方法里:

.run(function ($ionicPlatform, $ionicPopup, $rootScope, $location, $ionicHistory, $http, $ionicLoading, Storage, UtilService, Constant, $ionicActionSheet, $timeout, $cordovaAppVersion, $cordovaFileTransfer, $cordovaFile, $cordovaFileOpener2){
/**
     * android与IOS版本更新
     */
    $ionicPlatform.ready(function ($rootScope) {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleDefault();
        }
        checkUpdate();
        //检测更新
        function checkUpdate(){
            document.addEventListener("deviceready", getVersionUpdate, false); //更新的方法和函数$cordovaFileTransfer
        }
    });

    /**
     * 第一步访问后台拿到:版本信息
     */
    function getVersionUpdate(){
        UtilService.showLoading($ionicLoading);
        //访问网络拿到显示数据
        var url = Constant.url + "app/refresh/appUploadVersionControllerByApp.do";
        var username = UtilService.getMD5Str("APP_Refresh") + UtilService.getMD5Str("July_Three");
        $http({
            url: url,
            method: Constant.post,
            timeout: Constant.timeout,
            params: {
                username: DES3.encrypt(username,Constant.key),
                type: DES3.encrypt('商城',Constant.key)
            }
        }).success(function (data) {//响应成功
            UtilService.hideLoading($ionicLoading);
            var result = angular.fromJson(DES3.decrypt(data.data, Constant.key));
            if (result.flag === 'success') {
                var res =  result.result[0];
                console.log(res);
                onHardwareMenuKeyDown(res);
            } else {
                UtilService.showAlert($ionicPopup, "提示", "操作失败");
            }
        }).error(function (data, header, config, status) {//处理响应失败
            UtilService.hideLoading($ionicLoading);
            UtilService.showAlert($ionicPopup, "提示", "网络错误,稍后重试");
        });
    }

    /**
     * 第二步:拿到后台最新版本和本地版本进行比较进行决定是否更新
     */
    function onHardwareMenuKeyDown(versionMesage) {
        var type = navigator.connection.type; //获取网络的信息是通过cordova的插件来实现的
        //var serverAppVersion = "1.0.0"; //从服务端获取最新版本
        var serverAppVersion = versionMesage.VERSION_NAME; //从服务端获取最新版本
        //获取本地版本
        cordova.getAppVersion.getVersionNumber().then(function (version) {
            console.log(version);
            console.log(serverAppVersion);
            //如果本地与服务端的APP版本不符合
            if (version != serverAppVersion) {
                if (type === 'wifi') {
                    $ionicPopup.confirm({
                        title: '版本更新',
                        template: '<div style="text-align: center">发现新版本</div>',
                        cancelText: '稍后再说',
                        okText: '立即更新'
                    }).then(function (res) {
                        if (res) {
                            //如果是安卓平台,直接下载apk安装升级
                            if($cordovaDevice.getPlatform()=== 'Android'){
                                UpdateForAndroid(versionMesage.DOWNLOAD_PATH);
                                return;
                            }
                            //如果是IOS平台,更新升级需要跳转到苹果商店进行
                            if($cordovaDevice.getPlatform()=== 'iOS'){
                                UpdateForIOS();
                            }
                        } else {
                            //取消更新
                        }
                    });
                } else {
                    $ionicPopup.confirm({
                        title: '建议您在WIFI条件下进行更新,是否确认更新?',
                        template: '发现新版本',
                        cancelText: '稍后再说',
                        okText: '立即更新'
                    }).then(function (res) {
                        if (res) {
                            UpdateForAndroid(versionMesage.DOWNLOAD_PATH);
                        } else {
                            //取消更新
                        }
                    });
                }
            }
        });
    }
    //iOS版实现更新升级需要跳转到苹果商店进行
    function UpdateForIOS(){
        console.log('https://www.apple.com/');
        $window.open('https://www.apple.com/');//跳转到APP商店,这样即可
    }
    //android版本直接下载apk安装升级
    function UpdateForAndroid(downloadPath) {
        $ionicLoading.show({
            template: "已经下载:0%"
        });
        var url = Constant.url + downloadPath; //可以从服务端获取更新APP的路径
        var targetPath = "/sdcard/Dafa/mall.apk"; //APP下载存放的路径,可以使用cordova file插件进行相关配置
        var trustHosts = true;
        var options = {};
        console.log(url);
        $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) {
            console.log(err);
            alert('下载失败');
        }, function (progress) {
            //进度,这里使用文字显示下载百分比
            $timeout(function () {
                var downloadProgress = (progress.loaded / progress.total) * 100;
                $ionicLoading.show({
                    template: "已经下载:" + Math.floor(downloadProgress) + "%"
                });
                if (downloadProgress > 99) {
                    $ionicLoading.hide();
                }
            })
        });
    }
})
另加:数据库信息:
第三步:APP打包(签名打包):
	1、打开Android Studio,并且在Android Studio里选择file->Open打开platforms里的android
	2、点击Build:
3、Android签名打包详情去参照:http://blog.csdn.net/wwj_748/article/details/44622343
第四步:上传新版本:
1、定义版本号:

	2、上传新版本
	
	3、安装应用到手机,提示更新

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值