zoukankan      html  css  js  c++  java
  • Cordova app 检查更新 ----JS进行调用(二)

      1.获取版本号 需要添加 插件 

    cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git

    js 进行调用:
    // 获取当前移动设备已经安装的版本
    cordova.getAppVersion.getVersionNumber().then(function (version) {
        var versionCode = parseInt(version.toString().replace(/./g,''));
        console.log(versionCode);
        // 1.获取当前版本号
        // 2.获取服务器端的最新版本的数据源
        // 3.进行版本比较,如果当前的版本号与服务器的版本号不一致时,下载并安装最新的应用程序安装包
        Common.alert("温馨提示", "有新版本可供更新.
     1.界面美化 
     2.性能优化
     3.最新版本:"+versionCode);
    
    });

    2. 使用ajax进行获取服务中最新的安装包的详细信息

    3.比较版本号是否一致,如果不一致,将会提示是否进行“更新”

    4.如果选择"更新",对执行安装包的下载

    var url = "http://ftp-apk.pconline.com.cn/5014afca744eddb3fc7fe2d25d60aa29/pub/download/201010/HiMarket.apk?crazycache=1"; //可以从服务端获取更新APP的路径
    var targetPath = "HiMarket.apk"; //APP下载存放的路径,可以使用cordova file插件进行相关配置
    var trustHosts = true;
    var options = {};
    var ft = new FileTransfer();
    ft.download(url, targetPath, successCallback, fileTransfer.downloadFailed, trustHosts, options);

    5.将打开下载后的安装包

       Android 平台下打开安装包文件的方式与iOS的方式不一样

       Android使用本地路径进行打开

       iOS需要进行跳转到app store 中进行下载更新

       此时需要将FileOpener2.h中定义的方法添加到plugins.FileOpener2.js (iOS项目中的cordova-plugin-file-opener2目录下的www目录中)

       

    cordova.define("cordova-plugin-file-opener2.FileOpener2", function(require, exports, module) {
    /*jslint browser: true, devel: true, node: true, sloppy: true, plusplus: true*/
    /*global require, cordova */
    
    var exec = require('cordova/exec');
    
    function FileOpener2() {}
                   
    FileOpener2.prototype.openURL = function (url, callbackContext) {
    callbackContext = callbackContext || {};
    exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'openURL', [url]);
    };
    
    FileOpener2.prototype.open = function (fileName, contentType, callbackContext) {
        callbackContext = callbackContext || {};
        exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType]);
    };
    
    FileOpener2.prototype.uninstall = function (packageId, callbackContext) {
        callbackContext = callbackContext || {};
        exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'uninstall', [packageId]);
    };
    
    FileOpener2.prototype.appIsInstalled = function (packageId, callbackContext) {
        callbackContext = callbackContext || {};
        exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'appIsInstalled', [packageId]);
    };
    
    module.exports = new FileOpener2();
    });

      6.将这两种文件的打开方式进行封装成FileOpen.js

    /**
    *  使用方法:
    *  fileOpener.open(url).then(function(){});
    *  用到的原生api 有:cordova-plugin-dialogs,cordova-plugin-device,cordova-plugin-FileTransfer
    */
    
    /**FileTransfer*/
    var ft;
    Ext.define("util.FileOpen", {
        alternateClassName: "fileOpener",
        singleton: true,
        config: {
            isComplete: false,
            description: '高视医疗信息'
        },
        constructor: function (config) {
    
            this.initConfig(config);
        },
        // Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS
        getPromisedExec: function (command, success, fail) {
            var toReturn, deferred, injector, $q;
            if (success === undefined) {
                if (window.jQuery) {
                    deferred = jQuery.Deferred();
                    success = deferred.resolve;
                    fail = deferred.reject;
                    toReturn = deferred;
                } else if (window.angular) {
                    injector = angular.injector(["ng"]);
                    $q = injector.get("$q");
                    deferred = $q.defer();
                    success = deferred.resolve;
                    fail = deferred.reject;
                    toReturn = deferred.promise;
                } else if (window.Promise) {
                    toReturn = new Promise(function (c, e) {
                        success = c;
                        fail = e;
                    });
                } else if (window.WinJS && window.WinJS.Promise) {
                    toReturn = new WinJS.Promise(function (c, e) {
                        success = c;
                        fail = e;
                    });
                } else {
                    return console.error('AppVersion either needs a success callback, or jQuery/AngularJS/Promise/WinJS.Promise defined for using promises');
                }
            }
    
            if (device.platform == "Android") {
               
                cordova.plugins.fileOpener2.open(command, 'application/vnd.android.package-archive', success, fail);
            }
            else if (device.platform == "iOS") {
                // 加载其他应用,应用在升级的时候,iOS使用openURL打开、Android使用open打开  'tel://10086'
                cordova.plugins.fileOpener2.openURL(command, {
                    success: success,
                    error: fail
                });
            }
    
            return toReturn;
        },
        open: function (filePath, success, fail) {
            return this.getPromisedExec(filePath, success, fail);
        }
    });

     使用方法:

     1.比较版本号

        

    cordova.getAppVersion.getVersionNumber().then(function (version) {
               var versionCode = parseInt(version.toString().replace(/./g,''));
               // 此处写比较版本号逻辑,需要获取服务器上应用程序的最新版本号
    
           });

    2.下载安装包,并执行安装

           Progress.start("正在下载,请稍后...");
           // 下载安装包
           fileTransfer.download(function(entry){
    
               Progress.close();
    
               // 打开已经下载的安装包
               fileOpener.open(entry.fullPath).then(function(message){
                    console.log(message);
               },function(message){
                    console.log(message);
               });
    
               console.log('download complete: ' + entry.fullPath);
               console.log('download name: ' + entry.name);
    
           });

     


       
  • 相关阅读:
    UISlider
    App两个页面之间的正反传值方法
    UIImageview的简单运用
    UIPickerview 基本使用
    IOS开发中用开关(UISwitch)跟滑块(UISlider)控制手机屏幕的亮度
    冒泡排序
    简单抽屉实现
    iOS 模态视图,视图之间的切换
    UIScrollView和UIPageControl的使用(实现图片的循环滚动)
    iOS中UIPickerView实现省/市连动
  • 原文地址:https://www.cnblogs.com/ToFlying/p/5497992.html
Copyright © 2011-2022 走看看