Determining cordova compatibility version client-side


#1

The cordovaCompatibilityVersions.* attributes can be read from the manifest file with cordova-plugin-file

Anyone have an example of how to do this?


#2

Gonna give this a little bump


#3

I was searching for this too. I’m not sure if this is the cleanest method, but it should work - I’ve run it on android and ios devices successfully.

The Meteor Guide was leading me to look for manifest.json on the client. When I scanned the files on the devices in the program bundle, there were no __cordova/manifest.json files accessible via this plugin. However, I did find the cordovaCompatitiblityVersions in program.json.

The trick of this, is to find it… The file location is contingent on device platform and depending if you want to read the HCP or initial bundle version. I would think the initial bundle would suffice for this, as successful HCP installs should have the same cordova versions set anyway.

// Client-side code - .js

/**
 * Get file and read it
 * This is called by the resolveLocalFileSystemURL success handler
 * @param {FileEntry} fileEntry - a file/dir result from resolveLocalFileSystemURL
 */
const onFileEntrySuccess = (fileEntry) =>  {
  fileEntry.file((file) => {
    const fileReader = new FileReader();

    // Set the hook to handle the data
    fileReader.onloadend = function() {
      const { cordovaCompatibilityVersions } = JSON.parse(this.result);
      console.log(cordovaCompatibilityVersions);
    };

    // Finally, read the file!
    fileReader.readAsText(file);
  });
};

/**
 * Simple error output for callback
 * @param err
 */
const logError = err => console.error('logError(): ', err);

Meteor.startup(function(){
  // Must be in startup function to ensure cordova isReady
  if (Meteor.isCordova){
   // Get current version of app being served
   const { version } = __meteor_runtime_config__.autoupdate.versions['web.cordova'];

    // The directory is dependent on platform and whether we are looking for the program.json
    // from initial bundle, or subsequent HCPs.
    //  This setup will read initial bundle
   const baseDir = (cordova.platformId === 'ios') ?
        `${cordova.file.dataDirectory}meteor/${version}/program.json` : // ios initial and HCP (and android HCP?)
        `${cordova.file.applicationDirectory}www/application/program.json`; // android (initial bundle)

    // Access the native file system at the designated path 
    window.resolveLocalFileSystemURL(
      baseDir,
      onFileEntrySuccess,
      logError,
     );
  }
});

Helpful links used to get this far…
https://wicg.github.io/entries-api/
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#file-system-layouts