user2330237
user2330237

Reputation: 1703

Meteor Cordova App: how to run different code on iOS or Android

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.

Upvotes: 2

Views: 1463

Answers (4)

Kostadin Georgiev
Kostadin Georgiev

Reputation: 895

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...

Upvotes: 0

user2330237
user2330237

Reputation: 1703

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.

Upvotes: 0

Lucas
Lucas

Reputation: 10303

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()

Upvotes: 1

Simon Prickett
Simon Prickett

Reputation: 4148

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
}

Upvotes: 4

Related Questions