Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if running as "ionic serve" to use a conditional in the program

Is there a way to detect that ionic serve CLI is running (and then not on a real device) in the program and use it as a conditional.

My issue: I have a Cordova plugin that sends back an answer to Cordova.

When I was using Ripple, it offered to chose the value of the callback to be sent back to JavaScript (to emulate the plugin result).

I notice that Ionic2 does not do that when I run in a browser. So to ease my dev and make it possible to test it on a browser (and not build to the real device constantly), I would like to be able to check in the program if the ionic serve CLI is running. In other words: Check if it is running on a real device or a regular browser.

If it runs on the browser then I'd used a prompt to ask the user to enter a dummy value for the Cordova plugin result instead of the real Cordova plugin.

like image 782
nyluje Avatar asked Oct 28 '16 08:10

nyluje


1 Answers

See the Platform doc:

Check if it is running on a real device or a regular browser.

You can use the platform information in order to do so:

Platform Name   Description
android         on a device running Android.
cordova         on a device running Cordova.
core            on a desktop device.
ios             on a device running iOS.
ipad            on an iPad device.
iphone          on an iPhone device.
mobile          on a mobile device.
mobileweb       in a browser on a mobile device.
phablet         on a phablet device.
tablet          on a tablet device.
windows         on a device running Windows.

So you can do something like this:

import { Platform } from 'ionic-angular';

@Component({...})
export MyPage {
  constructor(public platform: Platform) {

    if (this.platform.is('mobileweb') || this.platform.is('core')) {
      // This will only print when running on desktop
      console.log("I'm a regular browser!");
    }
  }
}
like image 166
sebaferreras Avatar answered Nov 15 '22 09:11

sebaferreras