ionic 更新

翻阅了许多更新,发现有两种方式,一种是直接下载服务器的apk,一种是下载打包的www

第一种做完了,代码:

app.js

.run(function ($ionicPlatform, $rootScope, $location, $state, Storage,$ionicPopup,$ionicHistory,$timeout,$ionicActionSheet,$ionicLoading,$http) {
        $ionicPlatform.ready(function () {
            //在设备初始化完成后隐藏Splash画面延迟
            setTimeout(function() {
                navigator.splashscreen.hide();
            }, 1000);



            // 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 && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                cordova.plugins.Keyboard.disableScroll(true);
            }
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleLightContent();
            }
             // 菜单键 这个菜单键不知道是什么意思,拷贝的,留着了,也没删
            document.addEventListener("menubutton", onHardwareMenuKeyDown, false);
            //检测更新
            //热更新参考http://ionichina.com/topic/552b511e154e532908b07800  https://github.com/south-pacific/cordova-plugin-updateapp
            //更新www 参考http://www.scaperow.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89
            checkUpdate();
        });

这个方法就不解释了,拷贝的。不知道好用不

 // 菜单键
        function onHardwareMenuKeyDown() {
            $ionicActionSheet.show({
                titleText: '检查更新',
                buttons: [
                    { text: '关于' }
                ],
                destructiveText: '检查更新',
                cancelText: '取消',
                cancel: function () {
                    // add cancel code..
                },
                destructiveButtonClicked: function () {
                    //检查更新
                    checkUpdate();
                },
                buttonClicked: function (index) {

                }
            });
            $timeout(function () {
                hideSheet();
            }, 2000);
        };

重点来了

        // 检查更新
        function checkUpdate() {
            //从服务端获取最新版本
            var ApiUrl ="http://XXXXXXX/xxxxx/check_app_version";
            var serverAppVersion = ""; //最新版本信息
            $http.get(ApiUrl)
                .success(function (data) {
                    //获取版本
                    serverAppVersion = data.result;
                    cordova.getAppVersion.getVersionNumber().then(function (version) {
                        //如果本地与服务端的APP版本不符合                     
                        if (version != data.result.version) {
                            //两种更新方式
                            if(data.result.type=="large")
                            {
                                //大更
                                console.log("大");
                                showUpdateConfirm(serverAppVersion);
                            }
                            else
                            {
                                console.log("小");
                                //小更新
                            }
                        }
                    });
                })
                .error(function (data, status, headers, config) {
                    $ionicLoading.show({template: '读取版本信息失败!', noBackdrop: true, duration: 2000});
                });

        }

在这里参考了别的帖子:
phonegap100:
http://bbs.phonegap100.com/forum.php?mod=viewthread&tid=2046&highlight=%E8%87%AA%E5%8A%A8
ionichina:
http://ionichina.com/topic/552b511e154e532908b07800
github:
https://github.com/zxj963577494/ionic-AutoUpdateApp/blob/master/www/js/app.js
ionic.ren :志慧师兄
http://www.ionic.ren/2015/11/23/ionic%E5%B8%B8%E7%94%A8%E6%8F%92%E4%BB%B6-ngcordovacordovaappversion%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0%E6%8F%92%E4%BB%B6/
Ionic实战 自动升级APP(Android版)
http://www.cnblogs.com/zxj159/p/4421578.html
还有一个热心的兄弟提供的 : phoneGap中android程序自动更新代码
http://blog.csdn.net/binyao02123202/article/details/7955515

不管是谁复制谁的,都不好使,因为人家换方法了,你当然不好用了。下载人家旧的插件,当然就好用了。
下面主要是 fileTransfer.onprogress fileTransfer.download fileOpener2.open 这几个方法变了。
插件:

上传下载插件:cordova-plugin-file-transfer
https://github.com/apache/cordova-plugin-file-transfer 安装:cordova
安装:plugin add cordova-plugin-file-transfer

打开应用:cordova-plugin-file-opener2
https://github.com/pwlin/cordova-plugin-file-opener2 安装:cordova plugin
安装:cordova plugin add cordova-plugin-file-opener2

读取应用程序版本 cordova-plugin-app-version
https://github.com/whiteoctober/cordova-plugin-app-version
安装:cordova plugin add cordova-plugin-app-version

允许读写设备上的文件 org.apache.cordova.file
https://github.com/apache/cordova-plugin-file
安装:cordova plugin add cordova-plugin-file

服务器上的json
{
“version”:”0.0.2”,
“type”:”large”,
“url”:”http://xxx/MainActivity.apk“,
“file”:”file:///storage/sdcard0/Download/MainActivity.apk”,
“Description”:”新版功能1.asdasdas2.asdgasdgasd3.asdasd”
}

    // 显示是否更新对话框 大更
    function showUpdateConfirm(serverAppVersion) {
        var confirmPopup = $ionicPopup.confirm({
            title: '版本升级-'+serverAppVersion.version,
            template: serverAppVersion.Description, //从服务端获取更新的内容
            cancelText: '以后再说',
            okText: '升级'
        });
        confirmPopup.then(function (res) {
            if (res) {
                $ionicLoading.show({
                    template: "已经下载:0%"
                });
                var fileTransfer = new FileTransfer();
                var url = serverAppVersion.url; //可以从服务端获取更新APP的路径
                var targetPath = serverAppVersion.file;//APP下载存放的路径,可以使用cordova file插件进行相关配置
                //这个根据他的翻译
                // 可选参数,默认值为false。如果设置为true,它接受所有安全证书。这是非常有用的,因为Android拒绝签名安全证书。不建议用于生产使用。支持Android和iOS。(布尔)
                var trustHosts = true
                fileTransfer.download(
                    url,
                    targetPath,
                    function(entry) {
                        console.log("download complete: " + entry.toURL());
                        cordova.plugins.fileOpener2.open(
                            targetPath,
                            'application/vnd.android.package-archive',
                            {
                                error : function(e){
                                    console.log("错误=>"+'Error status: ' + e.status + ' - Error message: ' + e.message);
                                },
                                success : function(){
                                    console.log("下载成功");
                                }
                            }
                        );

                    },
                    function(error) {
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code" + error.code);
                    },
                    false ,
                    {
                        headers: {
                            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                        }
                    }
                );
                fileTransfer.onprogress= function(progressEvent) {
                    $timeout(function () {
                        var downloadProgress = (progressEvent.loaded / progressEvent.total) * 100;
                        $ionicLoading.show({
                            template: "已经下载:" + Math.floor(downloadProgress) + "%"
                        });
                        if (downloadProgress > 99) {
                            $ionicLoading.hide();
                        }
                    })

                };

            } else {
                // 取消更新 to do your code
            }
        });
    }

“`这里写图片描述
这里写图片描述
这里写图片描述

后面的小更,正在造。。
参考:
http://www.scaperow.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89


经过查资料,决定改变方向
http://www.phonegap100.com/plugininfo_46.html
微软的 更新插件
https://github.com/Microsoft/cordova-plugin-code-push

目前中文资料不太多
http://www.2cto.com/kf/201601/487903.html
还是暂时先放一放吧,项目目前可以不用这个功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值