2

I'm creating an omni-channel application using Kony and though it's all a single Javascript codebase, I'd like to conditionally execute some logic depending on whether the app is running on iOS, Android or a web browser. Something like:

if(isAndroid()) {
    //Do some stuff specific to Android.
}
else if(isIos()) {
    //Do some stuff specific to iOS.
}
else if(isWeb()) {
    //Do some stuff specific to Web.
}
Mig82
  • 4,856
  • 4
  • 40
  • 63

2 Answers2

4

Kony supports Preprocessor Directives such as #ifdef much like the C compiler's preprocessors. Since Kony projects are written in Javascript, these statements must be added in the form of special comments in order not to break the Javascript syntax. So for example #ifdef becomes //#ifdef.

These directives can be used to write code which gets built into the application or not depending on the host OS. So I've solved this by writing this:

var channel;
//#ifdef PLATFORM_NATIVE_IOS
channel = "ios"
//#endif
//#ifdef PLATFORM_NATIVE_ANDROID
channel = "android"
//#endif

And then writing the rest of my logic based on the value of my channel variable.

For a full list of the macros defined which you can use in these //#ifdef statements you can look at the first few lines in the kony_sdk.js module created by default in every Kony Visualizer project.

enter image description here

Mig82
  • 4,856
  • 4
  • 40
  • 63
1

Another solution is to rely on the kony.os.deviceInfo function from the kony.os namespace.

var deviceInfo = kony.os.deviceInfo();
var os = deviceInfo.name /*android and web*/ || deviceInfo.osname /*iOS*/;
if(os === "i-phone" || os === "i-pad"){
    //Do some stuff specific to iOS
}
else if(os === "android"){
    //Do some stuff specific to Android
}
else if(os === "thinclient"){
    //Do some stuff specific to web.
}

This is perhaps cleaner, but the result is that all the application logic gets bundled into every build regardless of which platform it's for. So this is only better if the amount of logic you want to run conditionally is small — Arguably because you don't want to pollute your Android codebase with a bunch of logic that will only execute on iOS or vice versa.

Mig82
  • 4,856
  • 4
  • 40
  • 63