2

We need different code to run on iOS or Android.

One way to do this would be to have different code in the package run at the outset (before all of the code runs) in the package files.

Meteor.isCordova allows the determination of whether on Cordova or browser.

How is it possible to determine, within Cordova, whether on iOS or Android.

Here is some code that doesn't work because Platform.isIOS() is not (yet) defined at this stage:

if (Meteor.isCordova && Platform.isIOS()) {
    Meteor.startup(function () {
        console.log('Using plugin for iOS');
    });
} else {
        console.log('Using plugin for Android');
}

This is not the same question asked here: PhoneGap - Detect device type in phonegap because the point is to detect this earlier in the process of a Meteor build (and also not using PhoneGap itself). The answers provided there do not work in the current context.

Community
  • 1
  • 1
user2330237
  • 1,629
  • 5
  • 20
  • 39

4 Answers4

4

Within Cordova you can use the Cordova Device Plugin to retrieve information about the device you are running on.

Once installed, you would use

device.platform

to determine which platform you are running on.

Example:

if (device.platform === 'Android') {
    // Android only code
}

if (device.platform === 'iOS') {
    // iOS only code
}
Simon Prickett
  • 3,838
  • 1
  • 13
  • 26
  • I am not able to get this to work, device is not defined until after onDeviceReady (which is not yet true at this stage, and I can't wait for that or the Device will never become ready so far as I can tell). Thanks. – user2330237 Nov 24 '15 at 16:44
  • Maybe try something like have a merge for each platform that changes some CSS class that you could use to work out what platform at runtime. Check out http://www.developer.com/ws/android/apache-cordova-using-merges-for-multiple-platforms.html for an example of setting up merge functionality in Cordova. Or use a pre build hook for each platform & have the hook script add say "ios" class to your HTML body for iOS and "android" for Android then use standard DOM API to work out which class body has & which platform you are on. https://cordova.apache.org/docs/en/dev/guide/appdev/hooks/ – Simon Prickett Nov 24 '15 at 16:48
1
Template.registerHelper('ios',function(){
  return ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
});
And another for Android:

Template.registerHelper('android',function(){
  return navigator.userAgent.toLowerCase().indexOf("android") > -1;
});

THEN

Blaze._globalHelpers.ios()
Blaze._globalHelpers.android()
Lucas
  • 9,871
  • 5
  • 42
  • 52
  • I am not able to get this to work. Template itself is not yet defined at this stage, so I get package.js:9:1: Template is not defined. Thanks for your help. – user2330237 Nov 24 '15 at 16:43
0

In the end, the best answer for this situation for us was to create two separate server instances of the app: one for ios and android. We then pointed the cordova version of the app on each type of device to the appropriate server instance and url (ios.server.com, android.server.com). They both pointed to the same Mongo database so all of the user information etc ends up in the same place.

This is not the most elegant solution, but it worked. Working through making the packages compatible with both platforms would ultimately probably be a better outcome, and we may do that, but this allowed us to get up and running.

This has the small side benefit of separating the load to the two instances and thus making it easy to handle the two situations separately and control and monitor their load separately through different server instances, etc.

user2330237
  • 1,629
  • 5
  • 20
  • 39
0

Create merges folder in main directory of the project(not in www) and inside it create iphone/android folders. Then add files that are platform depend and cordova will copy it when build your app.

If you have the same files in www directory you shoud create the new files with the same path for example merges/android/js/controllers/myController.js will replace the file for specific folder into www/js/controllers/myController.js

Note: If you want to replace only one function for example in the all file this can be done but is more tricky. To do this you should use Cordova Hooks. Read about them and create a script that will copy the specific parts of code into the files you want...

Kostadin Georgiev
  • 866
  • 1
  • 9
  • 23